Check Armstrong Number

Check whether a given integer is an Armstrong (narcissistic) number.

PythonBeginner
Python
# Program to check Armstrong number (order 3)

num = int(input("Enter an integer: "))

digits = str(num)
power = len(digits)

total = 0
for d in digits:
    total += int(d) ** power

if total == num:
    print(num, "is an Armstrong number")
else:
    print(num, "is not an Armstrong number")

Output

Enter an integer: 153
153 is an Armstrong number

An Armstrong number is equal to the sum of its digits each raised to the power of the number of digits. We convert the number to a string to iterate over digits, then compare the sum with the original number.