Python | Check Armstrong Number using for loop

Python program Check Armstrong Number using for loop

An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits.

Consider 153. It has 3 digits, so we raise each digit to the power of 3(the number of digits) and sum them up:
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

Checking Armstrong Number using for loop in Python

n = str(int(input("Enter an integer: ")))
digit_sum = 0
for i in n:
    digit_sum = digit_sum + int(i)**len(n)
if int(n) == digit_sum:
    print(n, "is an Armstrong number")
else:
    print(n, "is not an Armstrong number")

Output:

output

Also read:

Share:

Author: Yogesh Kumar