
Last Updated on 16 March 2025
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits.
For example, 153 is an Armstrong number because:
13 + 53 + 33 = 1 + 125 + 27 = 153
In this tutorial, we will write a Python program to check whether a number is an Armstrong number or not.
What is an Armstrong Number?
A number n is an Armstrong number if:
\[\text{sum of (each digit ^ total digits)} = \text{original number}\]Examples of Armstrong Numbers:
✔ 3-digit Armstrong numbers: 153, 370, 371, 407
✔ 4-digit Armstrong numbers: 9474, 8208
Python Program to Check Armstrong Number
# Taking user input
num = int(input("Enter a number: "))
# Convert number to string to count digits
num_digits = len(str(num))
# # Initialize variables
sum_of_powers = 0
temp = num
while temp > 0:
digit = temp % 10
sum_of_powers += digit ** num_digits
temp //= 10
# Check if it is an Armstrong number
if sum_of_powers == num:
print(num, "is an Armstrong number.")
else:
print(num, "is not an Armstrong number.")
Output 1
Enter a number: 153 153 is an Armstrong number.
Output 2
Enter a number: 123 123 is not an Armstrong number.
Check Armstrong Number for a Range of Numbers
If you want to find all Armstrong numbers within a given range, use this program:
# Taking user input
lower = int(input("Enter the lower range: "))
upper = int(input("Enter the upper range: "))
print(f"Armstrong numbers between {lower} and {upper} are:")
for num in range(lower, upper + 1):
num_digits = len(str(num))
sum_of_powers = sum(int(digit) ** num_digits for digit in str(num))
if sum_of_powers == num:
print(num, end=" ")
Output
Enter the lower range: 100 Enter the upper range: 1000 Armstrong numbers between 100 and 1000 are: 153 370 371 407