How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (2024)

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

Table of Contents

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_numberdigit, armstrong_sum = 0, 0length = 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: 371It'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.

How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (1)

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 = 0temp = input_numberwhile 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 : 407It 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 Armstrong Number In Python [with 5 Methods] - Python Guides (2)

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 == numberprint(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:

FalseTrueTrueFalse

You can refer to the screenshot below to see the result of the Python program after execution.

How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (3)

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 == numnum = 407if 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.

How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (4)

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:

  • How to Swap Two Numbers in Python Using Function
  • Swapping of Two Numbers in Python
  • Python Program to Find Product of Three Numbers
  • Traffic signal Program in Python

How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (5)

Bijay Kumar

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

How To Check Armstrong Number In Python [with 5 Methods] - Python Guides (2024)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6480

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.