Armstrong Numbers in Python: Check and Find Numbers in a Range

An Armstrong number (also called a narcissistic number) is a nonnegative integer equal to the sum of each digit raised to the power of the number of digits. For example, 153 = 1³ + 5³ + 3³ and 1634 = 1⁴ + 6⁴ + 3⁴ + 4⁴.

Interactive Companion Jupyter Notebook

Run the Python examples, compare expected outputs, modify the code, and complete guided exercises.

View on GitHub Open in Google Colab
On this page — Show page sections— Hide page sections

First working example

n = 153
digits = str(n)
total = sum(int(d) ** len(digits) for d in digits)
print("Sum of powered digits:", total)
print("Armstrong number:", total == n)

Output:

Sum of powered digits: 153
Armstrong number: True

This general expression works for numbers with any number of decimal digits.

Original three-digit example

For a three-digit input, the exponent is 3. The original example is retained with a clearer result and input validation.

raw = input("Enter a three-digit number: ").strip()
if not (raw.isdecimal() and len(raw) == 3):
    print("Enter exactly three digits")
else:
    n = int(raw)
    total = sum(int(d) ** 3 for d in raw)
    print("Sum of cubes:", total)
    print("Armstrong:", total == n)

Output:

Sum of cubes: 153
Armstrong: True

Check an Armstrong number of any length

Use len() to choose the exponent dynamically.

def is_armstrong(n):
    if n < 0:
        return False
    digits = str(n)
    return n == sum(int(d) ** len(digits) for d in digits)

for n in (0, 1, 153, 371, 1634, 9474, 100):
    print(n, is_armstrong(n))

Output:

0 True
1 True
153 True
371 True
1634 True
9474 True
100 False

List Armstrong numbers within a range

This keeps the original page’s range-search concept, including its expected four-digit matches.

def is_armstrong(n):
    digits = str(n)
    return n == sum(int(d) ** len(digits) for d in digits)
print([n for n in range(10, 10000) if is_armstrong(n)])

Output:

[153, 370, 371, 407, 1634, 8208, 9474]

Explain the digits of 1634

The fourth powers of 1, 6, 3 and 4 sum to 1634.

n = 1634
parts = [int(d) ** 4 for d in str(n)]
print(parts)
print(sum(parts))

Output:

[1, 1296, 81, 256]
1634

Handle nonnumeric and negative inputs

User input needs validation; a negative sign is not a digit.

raw = "-153"
if not raw.isdecimal():
    print("Enter a nonnegative whole number")
else:
    n = int(raw)
    print(n)

Output:

Enter a nonnegative whole number

Compare to the digit-cubes rule

A fixed exponent of three is not the definition of a general Armstrong number.

n = 1634
cubes = sum(int(d) ** 3 for d in str(n))
powers = sum(int(d) ** len(str(n)) for d in str(n))
print(cubes, powers)

Output:

308 1634

Common mistakes and troubleshooting

  • Run each example in Python 3.9 or newer. Indentation is significant in loops and functions.
  • Remember that input() returns a string. Convert and validate the value before doing arithmetic.
  • Check boundary values such as zero, negative numbers, small inputs and empty ranges.
  • When comparing outputs, note whether a code sample uses fixed values or user input.

Practice exercises

Check 153, 370, 371, 407, 1634 and 9474. Explain each exponent. Use the companion practice notebook for runnable examples and self-checks.

Frequently asked questions

Is 153 an Armstrong number?

Yes. 1³ + 5³ + 3³ = 153.

Is 1634 an Armstrong number?

Yes, using fourth powers.

Should I use exponent 3 for every number?

No. Use the number of digits as the exponent.

Plus2Net Python Basics podcast playlist

Other related resources

Back to top ↑




Subscribe to our YouTube Channel here



plus2net.com




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer