Check N-Digit Armstrong Numbers in Python

While I started teaching Python, one of the most common beginner questions I got was about Armstrong numbers.

It’s a simple concept, but it’s also a great way to practice loops, conditionals, and functions in Python.

In this tutorial, I’ll show you three different methods to check N-digit Armstrong numbers in Python. I’ll also provide complete code examples that you can run directly.

What is an Armstrong Number?

An Armstrong number (also called a narcissistic number) is a number that is equal to the sum of its own digits raised to the power of the number of digits.

For example:

  • 153 is an Armstrong number because:
    ( 1^3 + 5^3 + 3^3 = 153 )
  • 9474 is also an Armstrong number because:
    ( 9^4 + 4^4 + 7^4 + 4^4 = 9474 )

Method 1 – Use Loops in Python

When I first learned about Armstrong numbers, I started with a simple loop-based approach. This method is easy to understand and perfect for beginners.

Code:

# Method 1: Using Loops
def is_armstrong(num):
    digits = str(num)
    power = len(digits)
    total = 0

    for d in digits:
        total += int(d) ** power

    return total == num

# Example usage
number = 153
if is_armstrong(number):
    print(f"{number} is an Armstrong number.")
else:
    print(f"{number} is not an Armstrong number.")

Output:

153 is an Armstrong number.

You can refer to the screenshot below to see the output.

armstrong number in python

This method is simple and helps you clearly see how the calculation works. It’s great for small numbers and when you’re just starting.

Method 2 – Use Python List Comprehension

After I got comfortable with loops, I switched to Python’s list comprehensions. They make the code shorter and more Pythonic.

Code:

# Method 2: Using List Comprehension
def is_armstrong(num):
    digits = str(num)
    power = len(digits)
    return sum([int(d) ** power for d in digits]) == num

# Example usage
number = 9474
if is_armstrong(number):
    print(f"{number} is an Armstrong number.")
else:
    print(f"{number} is not an Armstrong number.")

Output:

9474 is an Armstrong number.

You can refer to the screenshot below to see the output.

what is armstrong number in python

This method is concise and efficient, making it perfect for quick checks. I often use it when I’m writing scripts that need to be clean and readable.

Method 3 – Find Armstrong Numbers in a Range

In real-world coding, I often need to check multiple numbers at once. This method lets you find all Armstrong numbers within a given range.

Code:

# Method 3: Find Armstrong Numbers in a Range
def armstrong_in_range(start, end):
    result = []
    for num in range(start, end + 1):
        digits = str(num)
        power = len(digits)
        total = sum(int(d) ** power for d in digits)
        if total == num:
            result.append(num)
    return result

# Example usage
print("Armstrong numbers between 100 and 10000 are:")
print(armstrong_in_range(100, 10000))

Output:

Armstrong numbers between 100 and 10000 are:
[153, 370, 371, 407, 1634, 8208, 9474]

You can refer to the screenshot below to see the output.

4 digit armstrong number

This method is practical when you want to explore Armstrong numbers across a range. It’s also handy for assignments or coding challenges.

Conclusion

In this tutorial, I showed you three different ways to check Armstrong numbers in Python:

  • Using loops for clarity.
  • Using list comprehension for clean, concise code.
  • Using a range-based function to find multiple Armstrong numbers.

Each method has its own use case, and I encourage you to try them all.
Once you’re comfortable, you can even extend these methods to larger numbers or optimize them for performance.

You may read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.