# Armstrong Number in Python

## Check Whether a Given Number is an Armstrong Number or Not in Python

For a given integer the objective is to check whether or not the given integer is an Armstrong number or not. The Armstrong number is briefly defined in the section below.

```Example
Input : 371
Output : It's an Armstrong Number
``` ## Check Whether a Given Number is an Armstrong Number or Not

Given an integer input, the objective is to check whether or not the given input variable is an Armstrong number. In order to do so, we check whether the sum of the digits of each number to the power the length of the number is equal to the number itself or not. If the number is the same as the original, it’s an Armstrong number. Mentioned below are a few of the Methods used to solve this problem,

• Method 1: Using Iteration
• Method 2: Using Recursion

We’ll see the working of the above mentioned methods in the upcoming sections. Please go through the below mentioned blue box for better understanding of the problem. ### Method 1: Using Iteration

In this method we’ll use for loop and while loop to check for Armstrong number.

### Working

For an integer input number, we perform the following operations

• We break down the number into digits using “%” operator.
• We raise the digit to the power of the length of the number.
• Keep appending the value to sum variable.
• Check if the sum variable matches the number itself.
• If the above condition is satisfied, it’s an Armstrong Number.

Let’s implement the above logic in Python Language.

### Python Code

Run
```number = 371
num = number
digit, sum = 0, 0
length = len(str(num))
for i in range(length):
digit = int(num%10)
num = num/10
sum += pow(digit,length)
if sum==number:
print("Armstrong")
else:
print("Not Armstrong")
```

### Output

```Armstrong
```

### Method 2: Using Recursion

In this method we’ll use recursion to check for Armstrong number. To know more about recursion, check out Recursion in Python.

### Working

For a given integer input, we do the following

• Define a recursive function checkArmstrong() with base case as num==0.
• Set the recursive step call as checkArmstrong(num/10, length, sum).

Let’s implement the above logic in Python Language.

### Python Code

Run
```number = 371
num = number
sum =0
length = len(str(num))
def checkArmstrong(num,length,sum):
if num==0:
return sum
sum+=pow(int(num%10),length)
return checkArmstrong(num/10,length,sum)

if checkArmstrong(num,length,sum)==number:
print('Armstrong')
else:
print("Not Armstrong")
```

### Output

```Armstrong
```

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

### 44 comments on “Armstrong Number in Python”

• # 13.Check Whether a Given Number is an Armstrong Number or Not in Python.

m=int(input(“Enter the num:”))
n=str(m)
a=len(n)
lst=[]
for i in n:
z=int(i)
fact=0
fact=z**a
lst.append(fact)
b=int(n)
sum=0
for j in lst:
sum=sum+j
if (sum==b):
print(f”{n} is a Armstrong.”)
else:
print(f”{n} is not a Armstrong.”) 0
• Prajwal

n = 1634
temp = n
find_len = len(str(n))
# print(find_len)
sumi =0
while n!=0:
last_dig=n%10
sumi += pow(last_dig,find_len)
n=n//10
if(sumi == temp):
print(“it is a armstrong number “)
else:
print(“not an armstrong number”) 0
• yaswanth

number = 371
num = number
sum = 0
length = len(str(num))
for i in range(length):
digit = num%10
num = num//10
sum =sum+(digit**length)
if sum==number:
print(“Armstrong”)
else:
print(“Not Armstrong”) 0
• Ganesh

num = int(input(“enter the num: “))
x = len(str(num))
sum = 0

for i in str(num):
num1 = int(i)**int(x)
sum = sum + num1

if sum == 370:
print(num,”is a armstrong number”)

else:
print(num,”is not a armstrong number”) 0
• NANDHAKUMAR

num=input(“Enter a number : “)
temp=num
c=0
for i in num:
c+=int(i)**len(num)
temp=int(i)//10
if num==c:
print(“Its a armstrong number”)
else:
print(“its not a armstrong number”) 0
• Sourabh

a=int(input(“Enter any number to check its armstrong or not: “))
b=str(a)
d=len(b)
sum=0
for i in b:
c=int(i)**d
sum+=c
if sum==a:
print(“its an armstrong number”)
else:
print(“its not an armstrong number”) 1
• Sahil

a = input()
n = int(a)
b = list(a)
c = len(a)
s = 0
for i in range(c):
f = int(b[i])
s += pow(f, c)

# print(s)
if s == n:
print(“ARM..”)
else:
print(“not ARM…”) 0