VNSGU BCA Sem 2: Programming Skills (204) Practical Solutions - April 2024 Set B
Paper Details
- Subject: Programming Skills Using C
- Subject Code: 204
- Set: B
- Semester: 2
- Month/Year: April 2024
- Max Marks: 25
- Time Recommendation: 2 Hours
- Paper: View Paper | Download PDF
Questions & Solutionsβ
All questions are compulsoryβ
Q1: Armstrong Number Checkβ
Max Marks: 20
Write a Python program to check whether the number is Armstrong or Not.
Hint
An Armstrong number is a number that is equal to the sum of cubes of its digits. For example, 153 = 1Β³ + 5Β³ + 3Β³ = 1 + 125 + 27 = 153.
Solution Approachβ
Solution - Python
def is_armstrong(number):
"""Check if a number is Armstrong number."""
original = number
sum_of_cubes = 0
# Calculate sum of cubes of digits
while number > 0:
digit = number % 10
sum_of_cubes += digit ** 3
number //= 10
return original == sum_of_cubes
# Main program
if __name__ == "__main__":
try:
# Get input from user
num = int(input("Enter a number: "))
if num < 0:
print("Please enter a positive number.")
else:
# Check and display result
if is_armstrong(num):
print(f"{num} is an Armstrong number!")
# Show calculation
print("\nCalculation:")
temp = num
calc_str = ""
while temp > 0:
digit = temp % 10
if calc_str:
calc_str = f"{digit}Β³ + " + calc_str
else:
calc_str = f"{digit}Β³"
temp //= 10
print(f"{calc_str}")
else:
print(f"{num} is NOT an Armstrong number.")
except ValueError:
print("Invalid input! Please enter a valid integer.")
Sample Output 1:
Enter a number: 153
153 is an Armstrong number!
Calculation:
1Β³ + 5Β³ + 3Β³
Sample Output 2:
Enter a number: 123
123 is NOT an Armstrong number.
Alternative: For n-digit Armstrong Numbers
The above solution works for 3-digit Armstrong numbers. For a general n-digit Armstrong number, use this approach:
def is_armstrong_general(number):
"""Check if a number is Armstrong number (general n-digit version)."""
original = number
digits = str(number)
n = len(digits)
sum_of_powers = 0
for digit in digits:
sum_of_powers += int(digit) ** n
return original == sum_of_powers
# Test with various Armstrong numbers
test_numbers = [153, 370, 371, 407, 1634, 8208, 9474]
for num in test_numbers:
result = "Armstrong" if is_armstrong_general(num) else "Not Armstrong"
print(f"{num}: {result}")
Alternative: Using List Comprehension
def is_armstrong_compact(number):
"""Compact version using list comprehension."""
digits = [int(d) for d in str(number)]
n = len(digits)
return sum(d ** n for d in digits) == number
# Main
num = int(input("Enter a number: "))
print(f"{num} is {'an' if is_armstrong_compact(num) else 'not an'} Armstrong number")
Q2: Viva Preparationβ
Max Marks: 5
Potential Viva Questions
- Q: What is an Armstrong number?
- A: A number equal to the sum of cubes of its digits (or nth power for n-digit numbers).
- Q: Why use
//instead of/in Python?- A:
//is integer division (floor division),/returns float.
- A:
- Q: What is the purpose of
**operator?- A: It is the exponentiation operator (e.g.,
digit ** 3means digitΒ³).
- A: It is the exponentiation operator (e.g.,
- Q: How do you reverse a number in Python?
- A: Use modulo
%to get last digit and integer division//to remove it.
- A: Use modulo
- Q: What are some Armstrong numbers?
- A: 0, 1, 153, 370, 371, 407 are 3-digit Armstrong numbers.
Common Pitfalls
- Single Digit: Remember 0-9 are all Armstrong numbers (1^n = 1).
- Negative Numbers: Armstrong numbers are defined for positive integers only.
- Type Conversion: Use
int()when converting string input to number.
Quick Navigationβ
Related Solutionsβ
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Current Page |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Solutions |
| Set F | Solutions |
Last Updated: April 2026