Armstrong Number Check

Check if a number is Armstrong number.

IntermediatePhase 2 - Level 2: Number-based Looping LogicExample 5 of 14
armstrong-number-check.py
1# Take number
2num = int(input("Enter a number: "))
3
4# Count digits
5temp = abs(num)
6count = 0
7while temp > 0:
8 count += 1
9 temp //= 10
10
11# Calculate sum of digits raised to power
12temp = abs(num)
13armstrong_sum = 0
14while temp > 0:
15 digit = temp % 10
16 armstrong_sum += digit ** count
17 temp //= 10
18
19# Check
20if abs(num) == armstrong_sum:
21 print("Armstrong number")
22else:
23 print("Not an Armstrong number")

Output

Enter a number: 153
Armstrong number

Enter a number: 123
Not an Armstrong number

What's going on

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