Python Program to Check Armstrong Number
153 = 1^3 + 5^3 + 3^3.Armstrong numbers (also called narcissistic numbers) are a popular practice problem because they combine several small skills: splitting a number into digits, counting digits, using powers and summing. Examples are 153, 370, 371 and 407 for three digits, and 1634, 8208 and 9474 for four.
The key detail is that the power is the number of digits, not always 3. A lot of tutorials hard-code 3 and then give wrong answers for other lengths. You will see two ways to extract digits: arithmetic with divmod, and converting to a string. Related digit exercises are the sum of digits program and the palindrome program.
| Input | A positive integer n |
|---|---|
| Output | Whether n is an Armstrong number (True/False) |
| Approach | Sum each digit raised to the digit count and compare with n |
| Time complexity | O(d) where d is the number of digits |
Approach (step by step)
- Count the digits of
n; call the countpower. - Take each digit in turn.
- Raise each digit to
powerand add it to a running total. - Compare the total with the original
n. If they match, it is an Armstrong number.
For 153 the count is 3, so the total is 1**3 + 5**3 + 3**3 = 1 + 125 + 27 = 153. For 154 the total would be 1 + 125 + 64 = 190, so it fails.
How the code works
The shortest way to get digits and a count is to turn the number into text with str(): len(str(n)) is the digit count, and iterating over the string yields each digit as a character that you convert back with int(). A generator expression inside sum() does the power and the addition in one line.
The arithmetic way avoids strings: divmod(n, 10) returns the rest of the number and the last digit, and a while loop repeats until nothing is left. Both give identical results; the string version is easier to read, the arithmetic one shows how digits really work.
n = 153
digits = [int(ch) for ch in str(n)]
print(digits, len(digits))
print([d ** len(digits) for d in digits])Variations: list every Armstrong number in a range
Wrap the check in a function and loop over a range to find all Armstrong numbers. Every single-digit number is trivially an Armstrong number (7 = 7^1), so many programs start the search from 10 or 100. Between 100 and 9999 the matches are 153, 370, 371, 407, 1634, 8208 and 9474.
There are only finitely many Armstrong numbers in base 10, and the largest has 39 digits, so a search eventually ends, but brute force becomes impractical long before then.
Examples you can run
Press Run, then edit the code and run it again.
Armstrong check with a generator expression
The function counts digits with len(str(n)) and compares the sum of powers with n.
def is_armstrong(n):
digits = str(n)
power = len(digits)
return n == sum(int(d) ** power for d in digits)
for n in [153, 154, 370, 9474, 9475]:
print(n, is_armstrong(n))Expected output153 True
154 False
370 True
9474 True
9475 False
Arithmetic version with divmod and all Armstrong numbers up to 10000
No string conversion: divmod(n, 10) peels off the last digit each time. The result list matches the known values.
def is_armstrong(n):
power = len(str(n))
total, rest = 0, n
while rest > 0:
rest, digit = divmod(rest, 10)
total += digit ** power
return total == n
found = [n for n in range(100, 10000) if is_armstrong(n)]
print(found)Expected output[153, 370, 371, 407, 1634, 8208, 9474]
Interactive version with input()
The typed text is converted with int() and the explanation shows the sum of powers.
n = int(input("Enter a number: "))
digits = [int(d) for d in str(n)]
total = sum(d ** len(digits) for d in digits)
if total == n:
print(f"{n} is an Armstrong number")
else:
print(f"{n} is not an Armstrong number (sum is {total})")Expected outputEnter a number: 407
407 is an Armstrong number
Common mistakes
Hard-coding the power as 3
✗ Wrong
n = 9474
total = sum(int(d) ** 3 for d in str(n))
print(total == n)✓ Right
n = 9474
total = sum(int(d) ** len(str(n)) for d in str(n))
print(total == n)Use the digit count as the power: len(str(n)).
Changing the number inside the loop and then comparing
✗ Wrong
n = 153
total = 0
while n > 0:
total += (n % 10) ** 3
n //= 10
print(total == n)✓ Right
n = 153
rest, total = n, 0
while rest > 0:
total += (rest % 10) ** 3
rest //= 10
print(total == n)Copy the number into another variable (for example rest) and keep n untouched for the final comparison.
Python Program to Check Armstrong Number: questions people ask
What is an Armstrong number in Python?
It is a number equal to the sum of its digits each raised to the power of the digit count, such as 153 (1^3 + 5^3 + 3^3) or 9474 (9^4 + 4^4 + 7^4 + 4^4).
How do you check an Armstrong number for any number of digits?
Use len(str(n)) as the exponent: n == sum(int(d) ** len(str(n)) for d in str(n)).
Is 1 an Armstrong number?
Yes. Every single-digit number equals itself raised to the power 1, so 0 to 9 are all Armstrong numbers.
Keep learning
Step-by-step lessons:
Python while LoopsPython for LoopsPython Numbers
Related:
Python Program to Find the Sum of Digits of a NumberPython Program to Check Palindrome (String and Number)Python Program to Check Prime NumberPython Program to Find Factorial of a Number
More in number programs:
Python Program to Check Even or Odd NumberPython Program to Check Prime NumberPython Program to Find Factorial of a NumberPython Program to Print the Fibonacci SeriesPython Program to Find GCD of Two NumbersPython Program to Check Leap YearPython Program to Find the Sum of Digits of a NumberPython Program to Print a Multiplication Table See all →