**Armstrong Number Program in Python** | In this post, we will discuss how to check Armstrong numbers in python with the explanation. Armstrong number is a number that when raised to the power of a number of its own digits is equal to the sum of that number.

**For example:-**

**(i)** Let’s assume a number as 5, the number of digits is 1 so 5 to the power of 1 is 5 there 5 is an Armstrong number.

**(ii)** 371 – there are 3 digits so, each digit will be raised to 3

3*3*3 + 7*7*7 + 1*1*1 = 371. Therefore 371 is an Armstrong number.

## Algorithm for Armstrong Number in Python

Now, let’s see the algorithm for the Armstrong number in python. An algorithm is a pseudo code to solve the problem, that is it is a step-by-step procedure.

**Step 1:** Take a number.**Step 2:** declare a variable to store the sum and initialize it to 0**Step 3:** find the count of digits in the given number**Step 4:** for each digit in a number multiply it to the count of digits and add it to the sum variable.**Step 5:** Check whether the given number and sum is equal or not**Step 6:** if both are equal then print “The number is an Armstrong” Else print “The number is not an Armstrong”.

**We will see these below Python program examples:–**

- Python program to check armstrong number using functions
- Python program to find armstrong number in an interval
- Armstrong number in python using for loop
- Armstrong number in python using while loop
- 3 digit Armstrong number in Python
- 4 digit Armstrong number in Python

## Python Program to Check Armstrong Number using Functions

We also call it the plus perfect number, a pluperfect digital invariant. In the code, we have defined two functions **digits_count()** and **sum()** which takes a single parameter number, the first function that is **digits_count(n)** is defined to count the number of digits in a given number. In the function, we use the while loop to check the number of digits. Next, in the second function **sum()** we take n as a parameter and call **digits_count()** to check the number of digits in the number, then in the while loop if the number is greater than 0 we,

**Step1:** we find mod 10 of the number and store it in digits.**Step2:** Then the number is divided by 10 and raise the individual number to the power of the number of its digits, add and store it in s**Step3:** We return the s.**Step4: **This process is iterated until the number is 0.

```
# Python program to check armstrong number
def digits_count(n):
i = 0
while n > 0:
n //= 10
i += 1
return i
def sum(n):
i = digits_count(n)
s = 0
while n > 0:
digit = n%10
n //= 10
s += pow(digit,i)
return s
# take inputs
num = int(input("Enter number: "))
# calling function
s = sum(num)
# check armstrong number or not
if s == num:
print("The number is an Armstrong number.")
else:
print("The number is not an Armstrong number.")
```

Output for the input values test-case-1:-

Enter number: 5

The number is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 12

The number is not an Armstrong number.

## Python Program to Find Armstrong Number in an Interval

We can also set intervals to find the Armstrong number; this works as follows. Python code takes two intervals, upper limit and lower limit, and checks for the Armstrong number in between them.

```
# Python program to find armstrong number in an interval
# take range
low = 1
up = 50
# find armstrong number in range
for i in range(low, up +1):
pow = len(str(i))
sum = 0
temp = i
while temp > 0:
digits = temp %10
sum += digits ** pow
temp //= 10
if i == sum:
print(i)
```

Output:

1

2

3

4

5

6

7

8

9

In the code, we have taken an interval from 1 to 50 so the output will be the Armstrong number between 1 to 50, We use a for loop to iterate the number between the range and then find the Armstrong number between the specified range.

## Armstrong Number in Python using For Loop

In the previous program, the range is hardcoded in the program but in this program, we will find the Armstrong number in an interval given by the user.

```
# Python program to find armstrong number in range
# take range
low = int(input("Enter the lower limit: "))
up = int(input("Enter the upper limit: "))
# find armstrong number in range
for i in range(low, up +1):
pow = len(str(i))
sum = 0
temp = i
while temp > 0:
digits = temp %10
sum += digits ** pow
temp //= 10
if i == sum:
print(i)
```

Output for the input values test-case-1:-

Enter the lower limit: 100

Enter the upper limit: 1000

153

370

371

407

In the example given we have set the lower limit as low to 100 and the upper limit as up to 1000 then by using a for loop we find the Armstrong number between 100 and 1000 and print the same.

## Armstrong Number Program in Python using While Loop

Let’s see a python program to check Armstrong number using a while loop, while loop is a loop that executes until the condition is false, once the condition becomes false flow comes out of the loop. To find the Armstrong number we need to repeat the same steps several times until the number becomes 0 so we use a while loop.

```
# Python program to check armstrong number
# take inputs
number = int(input("Enter number: "))
# find armstrong number
number_new = number2 = number
sum = 0
count = 0
while(number > 0):
count = count + 1
number = number//10
while number2 > 0:
remainder = number2 % 10
sum += remainder ** count
number2 //= 10
# check armstrong number or not
if(number_new == sum):
print("The number is an Armstrong number.")
else:
print("The number is not an Armstrong number.")
```

Output for the input values test-case-1:-

Enter number: 153

The number is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 250

The number is not an Armstrong number.

## 3 Digit Armstrong Number in Python

Armstrong number python for 3 digits is the condition where we find only 3 digit Armstrong numbers.

```
# Python program to check 3 digit armstrong number
# take inputs
number = int(input("Enter number: "))
# find armstrong number
temp = number
sum = 0
while temp != 0:
digits = temp % 10
sum += digits * digits * digits
temp = temp // 10
# check armstrong number or not
if sum == number:
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")
```

Output for the input values test-case-1:-

Enter number: 370

370 is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 5

5 is not an Armstrong number.

Any Armstrong number other than 3 digits Armstrong number will be printed as “not an Armstrong number” that is, the above code evaluates only 3 digits of Armstrong number.

## 4 Digit Armstrong Number in Python

Armstrong number python for 4 digits is the condition where we find only 4 digits of the Armstrong number.

```
# Python program to check 4 digit armstrong number
# take inputs
number = int(input("Enter number: "))
# find armstrong number
temp = number
sum = 0
while temp != 0:
digits = temp % 10
sum += digits * digits * digits * digits
temp = temp // 10
# check armstrong number or not
if sum == number:
print(number, "is an Armstrong number.")
else:
print(number, "is not an Armstrong number.")
```

Output for the input values test-case-1:-

Enter number: 1634

1634 is an Armstrong number.

Output for the input values test-case-2:-

Enter number: 153

153 is not an Armstrong number.

Output for the input values test-case-3:-

Enter number: 5

5 is not an Armstrong number.

Any Armstrong number other than 4 digits Armstrong number will be printed as “not an Armstrong number” that is the above code evaluates only 4 digits Armstrong number.

Also See:- Perfect Number Program in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!