This post shows how to check whether a number is an Armstrong number in Python. An Armstrong number is a number that equals the sum of its own digits, each raised to the power of the total number of digits.
Check Armstrong number in Python:
Explanation:
We convert the number to a string to count its digits, then raise each digit to that power and sum the results. If the sum equals the original number, it's an Armstrong number — for example, 153 = 1³ + 5³ + 3³.
Python Program to Check Armstrong Number
num = int(input("Enter a number: "))
digits = str(num)
power = len(digits)
total = sum(int(d) ** power for d in digits)
if total == num:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
Output:
Enter a number: 153
153 is an Armstrong number
You can run and tweak this example live in our Code Runner.
