Have you ever wondered what is the Armstrong number in Python? Armstrong number in Python is a special sequence, this number is important for a variety of reasons, and can be used in a variety of different contexts.
In this article, we’ll explain the Python program to check armstrong number. So whether you’re a student or a professional mathematician, read on to learn more.
Table of Contents
What is Armstrong Number?
Armstrong number is invented by Edward Howard Armstrong. Armstrong is such number, when the n power of digits being added gives the result as the number itself.
Armstrong number is a positive integer of order n such that:
abc = an + bn + cn
Examples
371 = 3*3*3 + 7*7*7 + 1*1*1 // 371 is an Armstrong number
1634 = 1*1*1*1+ 6*6*6*6 + 3*3*3*3 + 4*4*4*4 // 1634 is an Armstrong number
Python Program To Check Armstrong Number : 2 Methods
Armstrong number is also a Narcissistic number and is just a mathematical puzzle. We have two different methods to code concept of Armstrong number in Python.
- While Loop
- For Loop
Here we’ll discuss them in detail with the help of algorithm.
Algorithm for Armstrong Number in Python
- Enter number n
- Calculate the total number of digits and store it as “a”
- Create and declare variable add and set it value as 0.
- Calculate the cube of each digits and sum them.
- Check whether the calculated sum is equal to input number.
- If the sum or the input number is same the number is Armstrong. If not, number is not Armstrong.
Also Read, Python program to check Leap Year.
1. Armstrong Number in Python Using While Loop
While loop is an important part of Python programming language. It allows you to repeat a block of code while a certain condition is met. This can be useful for performing tasks over and over again, or checking the status of something before proceeding with your program.
By using while conditional statement we will check whether the user’s input is an Armstrong number or not.
Consider the following code example to get better understanding.
num = int( input("Enter a number: ") )
# initialize sum
sum = 0
# find the sum of the cube of each digit
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
# display the result
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Output


2. Armstrong Number in Python Using For Loop
For loop in python can be used for a number of purposes, one of which is to iterate through a list or collection. This type of loop is often used when you want to perform certain tasks on each element in the list, or when you need to run multiple commands as part of your script.
Let’s learn the following example to understand how to find armstrong number using for loop.
from re import T
n = int(input("\nPlease Enter the Value : "))
Sum = 0
T = 0
# T=> time count
Temp = n
while Temp > 0:
T = T + 1
Temp = Temp // 10
Temp = n
for a in range(1, Temp + 1):
Reminder = Temp % 10
Sum = Sum + (Reminder ** T)
Temp //= 10
if n == Sum:
print("\n %d is Armstrong.\n" %n)
else:
print("\n %d is Not Armstrong.\n" %n)
Output

FAQs
How many Armstrong numbers are there between 1 and 10000?
The armstrong numbers between 1 and 10000 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474.
Can negative number be armstrong numbers?
No, negative integers can not be included in armstrong numbers. It will prompt an error message.
Learn about Python Anagrams.
Conclusion
This article discussed all about python program to check armstrong number. We came up with different methods like while loop and for loop for easy approach. Hope this will article will help you in better understanding the concept of Armstrong number. Thanks for reading.