Python Program to check Armstrong number
Given a number , Write a program in python to check whether it is Armstrong or not . So first we have to know that what is Armstrong number .
Armstrong number:-
1) 153=(1)^3 + (5)^3 +(3)^3
= 1+125+27
= 153
So 153 is a Armstrong number
2) 12=(1)^2 +(2)^2
=1+4
=5
So 12 is not Armstrong number
So first of all calculate the number of digit and then separate digits one by one from number by finding reminder and and calculate power of that digit with number of digit that is pow(digit, number_o_digit), and at last compare sum with number if equal then Armstrong otherwise not.
n=int(input("Enter a number"))
no_of_digit=0
'''number stored in another variable for use because after first
loop number become zero'''
new_num=n
while n!=0:
no_of_digit+=1
n=n//10
sum=0
'''We store value of new_num in n because n is zero before
this step so we can not compare'''
n=new_num
while new_num!=0:
r=new_num%10
mul=1
j=0
for j in range(no_of_digit):
mul=mul*r
sum=sum+mul
new_num=new_num//10
if sum==n:
print("Armstrong number")
else:
print("Not Armstrong number")
Output:
Enter a number
123
Not Armstrong number
0 Comments