An Armstrong number is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 153 has 3 digits, and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so 153 is an Armstrong number. In this lesson you will write a Python program to check an Armstrong number and to list all of them up to 1,000.

Run the code in our online Python compiler. The compiler does not take keyboard input, so the examples use fixed values.

 

The logic

  1. Count the digits of the number. Call this count p.
  2. Raise each digit to the power p.
  3. Add the results.
  4. If the sum equals the original number, it is an Armstrong number.

 

Program: check one number

def is_armstrong(n):
    digits = str(n)
    power = len(digits)
    total = sum(int(d) ** power for d in digits)
    return total == n

for number in [153, 370, 123, 9474, 5]:
    print(number, is_armstrong(number))

Output:

153 True
370 True
123 False
9474 True
5 True

How it works

str(n) turns the number into text so we can go through its digits one by one. len(digits) gives the number of digits. For each digit d, int(d) ** power raises it to that power, and sum() adds them up.

Number

Digits

Calculation

Sum

Armstrong?

153

3

1³ + 5³ + 3³ = 1 + 125 + 27

153

Yes

370

3

3³ + 7³ + 0³ = 27 + 343 + 0

370

Yes

123

3

1³ + 2³ + 3³ = 1 + 8 + 27

36

No

9474

4

9⁴ + 4⁴ + 7⁴ + 4⁴ = 6561 + 256 + 2401 + 256

9474

Yes

Every single-digit number is an Armstrong number, because a digit raised to the power 1 is the digit itself.

 

Program: all Armstrong numbers from 1 to 1000

Add this below the is_armstrong() function:

result = [n for n in range(1, 1001) if is_armstrong(n)]
print(result)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]

Another way: using a while loop

This version works with numbers, without converting to a string.

def is_armstrong_loop(n):
    power = len(str(n))
    original = n
    total = 0
    while n > 0:
        digit = n % 10
        total += digit ** power
        n = n // 10
    return total == original

print(is_armstrong_loop(153))
print(is_armstrong_loop(154))

Output:

True
False

n % 10 gives the last digit, and n // 10 removes it. We save the original number first, because n changes inside the loop.

 

Take the number from the user (on your computer)

number = int(input('Enter a number: '))
if is_armstrong(number):
    print(number, 'is an Armstrong number')
else:
    print(number, 'is not an Armstrong number')

Our online compiler does not support input() yet, so use a fixed value there.

 

Common mistakes

  • Always using the power 3. The power equals the number of digits, so 9474 uses the power 4.
  • Forgetting to save the original number in the while-loop version.
  • Comparing the sum with a string. Compare numbers with numbers, not with str(n).

 

Practice questions

  1. Check whether 407 and 1634 are Armstrong numbers.
  2. Print all three-digit Armstrong numbers.
  3. Count how many Armstrong numbers there are below 10,000.
  4. Write a function that returns the sum of the cubes of the digits of a number.

 

FAQs

What are the three-digit Armstrong numbers?

153, 370, 371 and 407.

Is 1 an Armstrong number?

Yes. 1 has one digit, and 1¹ = 1.

Is an Armstrong number the same as a narcissistic number?

Yes. Narcissistic number is another name for it.