Armstrong number in Python
Written by
Armstrong Number Program in Python
In this program where we are going to learn whether an N-digit integer is an Armstrong number or not.
A number is said to be an Armstrong number if it is equal to the sum of the cubes of its own digit.
Example: 153 is an Armstrong number, 153=1*1*1 + 5*5*5 + 3*3*3
Algorithm:
- Input from the user.
- Take the length of the number.
- Assign a = n
- Initialize arm = 0.
- Using for loop, find whether a given number is Armstrong or not.
- Using if condition, if arm !=n then print it is not an Armstrong.
- Else print it is an Armstrong number.
- Exit
Code:
n=input("Enter a number :")
l=len(n)
n=int(n)
a=n
arm=0
for i in range(l+1):
b=a%10
a=a/10
arm=arm+(b**l)
if (arm != n):
print("It is not an Armstrong no.")
else:
print("It is an Armstrong no.")
Output:
Enter a number :153
It is an Armstrong no.