Python program to check Armstrong number


What is an Armstrong number?

Armstrong number is an n digits numbers which is equal to the sum of the nth power of its digits. For example 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, etc.

Python Code:

num = int(input("Enter a number:"))
sum = 0
temp = num
n = len(str(num))    # Counting number of digits.
while temp > 0:
    digit = temp%10
    sum += digit**n
    temp //= 10
if num == sum:
    print(num,"Is an Armstrong number")
else:
    print(num,"Is not an Armstrong number")

Output:

Enter a number: 370

370 Is an Armstrong number


38 views0 comments

Recent Posts

See All