How to Check Armstrong Number in Python [with 5 Methods]

Do you want to learn how to check Armstrong number in Python? Let me tell you the various methods to write a Python program to check Armstrong number.

An Armstrong number in Python is a special number that equals the sum of its digits, each raised to a power of the number of its digits. It is also known as a narcissistic number.

For Example:

  • Take the number and separate its digits like 370, we get the digits 3, 7, and 0.
  • Next, assign each digit to a power equal to the number of digits in the given number. Here, three digits are there, so each digit gets the power 3, such as 3^3, 7^3, and 0^3.
  • Now, calculate the result of each digit like 3^3 = 27, 7^3= 343, and 0^3 = 0. Lastly, add the results of all the three digits: 27+343+0 = 370.

If the sum of these calculated results equals the original number, i.e., 370, then that number is an Armstrong number.

In this Python article, we will cover several methods with examples to check Armstrong numbers in Python.

Here is the list of methods to check Armstrong numbers in Python.

  • Using for loop
  • Using while loop
  • Using list comprehension
  • Using function
  • Using recursion

Armstrong number in Python using for loop

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

Here is the complete code to check Armstrong number in Python using a for loop.

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.

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.

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 image below. This provides the result of the Python program to check the Armstrong number using a for loop.

Python Program to Check Armstrong Number

How to Check Armstrong Number in Python Using While Loop

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. I have applied the modulo operator(%) to extract the last digit of temp. Then, the (*) operator in Python is used to calculate the cude of the current digit, and integer division by 10 is done to remove the last digit from temp.

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

Refer to the screenshot below, this will provide the result of the Python program to check the Armstrong number.

How to check whether it is an Armstrong number or not in Python using a while loop

Check Armstrong Number in Python Using List Comprehension

This is another way to check the Armstrong number in Python using 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.

Here is the complete source code of Python to check Armstrong numbers using list comprehension.

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 digits contain the individual digits of the input number.

Then, 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.

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 result of the Python program after execution.

Armstrong number in Python using list comprehension

Python Program to Check Armstrong Number Using Function

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.

The source code of Python to check the Armstrong number using a function.

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.

Refer to the image below. It provides the result of the Python program to check the Armstrong number using a function.

Python program to check Armstrong number using function

Armstrong Number in Python using Recursion

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

How to find Armstrong Number in Python using Recursion

Conclusion

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

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

You may also like to read the following articles: