How to Check Armstrong Number in Python

I was working on a coding exercise where I had to check if a number was an Armstrong number. At first, it seemed like a simple task, but I realized many beginners struggle with the concept.

As someone who has been coding in Python for more than 10 years, I’ve often seen Armstrong number problems pop up in interviews and coding tests. So, I decided to write this tutorial to break it down in the simplest way possible.

In this article, I’ll show you how to check Armstrong number in Python using different methods. I’ll also share the complete code so you can try it on your own system.

Method 1: Use a Python for loop

A for loop in Python is used to iterate over a sequence or collection. It does not require an indexing variable to be set beforehand.

Check_Armstrong_number = int(input("Enter a number: "))
num = Check_Armstrong_number
digit, armstrong_sum = 0, 0

length = len(str(num))
for i in range(length):
    digit = num % 10
    num = num // 10
    armstrong_sum += pow(digit, length)

if armstrong_sum == Check_Armstrong_number:
    print("It's an Armstrong number:", Check_Armstrong_number)
else:
    print("Not an Armstrong number:", Check_Armstrong_number)

Here, this loop iterates over each digit of the number and then uses the modulo (%) operator in Python. It will extract the last digit of the given number.

for i in range(length):
    digit = num % 10
    num = num // 10
    armstrong_sum += pow(digit, length)

Output:

Enter a number: 371
It's an Armstrong number: 371

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

Python Program to Check Armstrong Number

After that, I applied the integer (//) operator in Python that will remove the last digit, and then each digit raised to the power of the length of the number is added to the armstrong_sum.

Method 2: Use a While Loop in Python

In Python, the while loop executes a set of statements if the condition is True. This is the Python source code to check the Armstrong number in Python using a while loop.

input_number = int(input("Enter a number : "))
sum_of_armstrong = 0
temp = input_number

while temp > 0:
    digit = temp % 10
    sum_of_armstrong += digit * digit * digit
    temp = temp//10
 
if sum_of_armstrong==input_number:
    print('It is an Armstrong number')
else:
    print('It is not an Armstrong number')

Here, the while loop in Python continues until the temp value becomes 0.

while temp > 0:
    digit = temp % 10
    sum_of_armstrong += digit * digit * digit
    temp = temp//10

Output:

Enter a number : 407
It is an Armstrong number

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

Check Armstrong number in Python using while loop

I have applied the modulo operator(%) to extract the last digit of temp. Then, the (*) operator in Python is used to calculate the cube of the current digit, and integer division by 10 is done to remove the last digit from temp.

Method 3: Use List Comprehension

List comprehension in Python offers a shorter syntax when we want to create a new list based on the values of an existing list.

def is_armstrong(number):
    digits = [int(digit) for digit in str(number)]
    sum_of_cubes = sum([digit**len(digits) for digit in digits])
    return sum_of_cubes == number

print(is_armstrong(126))
print(is_armstrong(371))
print(is_armstrong(9474))
print(is_armstrong(123))

Here, the list comprehension helps us to iterate over each character (digit) in the Python string representation of the number, and the resulting list of digits contains the individual digits of the input number.

digits = [int(digit) for digit in str(number)]
sum_of_cubes = sum([digit**len(digits) for digit in digits])

Output:

False
True
True
False

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

Armstrong number in Python using list comprehension

This calculates the sum of the cube of each digit on the list of digits by using list comprehension to iterate over each digit in the digit list in Python.

Method 4: Use a Function in Python

A function in Python is a code block that runs only when called. We can also use a function to check the Armstrong number in Python.

def check_armstrong_number(num):
    num_str = str(num)
    num_digits = len(num_str)
    armstrong_sum = sum(int(digit) ** num_digits for digit in num_str)
    return armstrong_sum == num

num = 407
if check_armstrong_number(num):
    print(f"{num} is an Armstrong number.")
else:
    print(f"{num} is not an Armstrong number.")

Output:

407 is an Armstrong number.

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

Python program to check Armstrong number using function

Using a function makes it easy to check whether a number is an Armstrong number by encapsulating the logic for reuse and clarity.

Armstrong Number in Python using Recursion

Suppose you want to check the Armstrong numbers in Python using recursion. You can refer to the link below.

Find Armstrong Number in Python using Recursion

I hope you find this Python tutorial useful, as it taught you the various methods to write a Python program to check an Armstrong number with examples.

We can use the loops in Python, such as the for loop and while loop, a list comprehension, and we can also use functions such as a custom function or recursion to check Armstrong numbers.

You may also like to read the following articles:

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.