Check Armstrong Number in Python
Check whether a given integer is an Armstrong (narcissistic) number.
BeginnerConditional ProgramsExample 6 of 20
check-armstrong-number.py
1# Program to check Armstrong number (order 3)23num = int(input("Enter an integer: "))45digits = str(num)6power = len(digits)78total = 09for d in digits:10 total += int(d) ** power1112if total == num:13 print(num, "is an Armstrong number")14else:15 print(num, "is not an Armstrong number")
Output
Enter an integer: 153 153 is an Armstrong number
What's going on
An Armstrong number is equal to the sum of its digits each raised to the power of the number of digits.
We convert the number to a string to iterate over digits, then compare the sum with the original number.
After this program
If the idea is still fuzzy, read Day 8: Conditional Statements in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 20 conditional programs, or see every topic on Python exercises.