Given an integer,Write a python program to check if an integer is an Armstrong or not.
Armstrong
A given a number is said to be armstrong if the number of order or length n is digit*n times and the sum is equal to the number given
example - 153 = 1*1*1 + 5*5*5 +3*3*3
Example:
1. 153 - Armstrong
2. 345 - Not armstrong
3. 1634 - Armstrong
Python Program
#Check whether the given integer is an armstrong or not
#function for armstrong
def armstrong(n):
#convert into str to calculate the order of the digit
a = str(n)
temp,sum= n,0
while temp>0:
#modulo by 10 to get each and every digit of a number
digit = temp%10
#multiply each digit with the order of the digit and sum the values
sum+=digit**len(a)
temp//=10
#Print the result
print("Armstrong") if sum==n else print("Not armstrong")
n = int(input())
#calling the armstrong function
armstrong(n)
Input:
153
Output:
Armstrong
Input:
345
Output:
Not armstrong
Comments
Post a Comment