Armstrong Number Check

Check if a number is Armstrong number.

Logic BuildingIntermediate
Logic Building
# Take number
num = int(input("Enter a number: "))

# Count digits
temp = abs(num)
count = 0
while temp > 0:
    count += 1
    temp //= 10

# Calculate sum of digits raised to power
temp = abs(num)
armstrong_sum = 0
while temp > 0:
    digit = temp % 10
    armstrong_sum += digit ** count
    temp //= 10

# Check
if abs(num) == armstrong_sum:
    print("Armstrong number")
else:
    print("Not an Armstrong number")

Output

Enter a number: 153
Armstrong number

Enter a number: 123
Not an Armstrong number

Armstrong: sum of digits raised to power of digit count equals the number.

Key Concepts:

  • First count digits
  • Then calculate sum of digits^count
  • Compare with original number