Learnitweb

Python Program to check Armstrong Number

A positive integer of n digits is an Armstrong number of order n (order is number of digits) if

abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n)

For example, 153 is an Armstrong number.

153 = 1^3 + 5^3 + 3^3

num = int(input("Please provide number: "))

# Changed number to str and then it's length
# to calculate order
order = len(str(num))

#initialize sum to 0
sum = 0

# find the sum of the digit to the power of order
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** order
   temp //= 10

# display the result
if num == sum:
   print(num,"is an Armstrong number")
else:
   print(num,"is not an Armstrong number")

Output

Please provide number: 153
153 is an Armstrong number
Please provide number: 121
121 is not an Armstrong number