Skip to content

VNSGU BCA Sem 2: Programming Skills (204) Practical Solutions - 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

flowchart TD
    start[Start] --> input[Input Number]
    input --> init[Initialize sum = 0]
    init --> temp[Store number in temp]
    temp --> loop{Temp > 0?}
    loop -->|Yes| digit[Extract last digit<br/>digit = temp % 10]
    digit --> cube[Add cube to sum<br/>sum += digit³]
    cube --> reduce[Remove last digit<br/>temp = temp // 10]
    reduce --> loop
    loop -->|No| check{sum == original?}
    check -->|Yes| armstrong[Print Armstrong]
    check -->|No| notarmstrong[Print Not Armstrong]
    armstrong --> end[End]
    notarmstrong --> end
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
  1. Q: What is an Armstrong number?
  2. A: A number equal to the sum of cubes of its digits (or nth power for n-digit numbers).
  3. Q: Why use // instead of / in Python?
  4. A: // is integer division (floor division), / returns float.
  5. Q: What is the purpose of ** operator?
  6. A: It is the exponentiation operator (e.g., digit ** 3 means digit³).
  7. Q: How do you reverse a number in Python?
  8. A: Use modulo % to get last digit and integer division // to remove it.
  9. Q: What are some Armstrong numbers?
  10. 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

Set Link
Set A Solutions
Set B Current Page
Set C Solutions
Set D Solutions
Set E Solutions
Set F Solutions

Last Updated: April 2024