Python Program to Check Armstrong Number
A number is an Armstrong number when the sum of its digits, each raised to the power of the number of digits, equals the number itself.
The question
Read a positive integer and print whether it is an Armstrong number: the sum of each digit raised to the power of the number of digits equals the number itself (153 = 1³ + 5³ + 3³).
The code
program.py
n = int(input())
k = len(str(n))
total = 0
temp = n
while temp > 0:
total += (temp % 10) ** k
temp //= 10
if total == n:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")Input
123
Output
123 is not an Armstrong number
How it works
- 1k = len(str(n)) counts the digits — 3 for 153, 4 for 1634.
- 2Work on a copy (temp) so n is still available for the final comparison.
- 3Add (temp % 10) ** k for every digit, removing digits with temp //= 10.
- 4Compare the total with n.
Common mistakes
- Always cubing (** 3) — it works for 153 but fails for 4-digit Armstrong numbers like 1634.
- Changing n itself in the loop, so the final comparison is against 0.
Armstrong number in Python using a for loop
Turn the number into a string and the for loop hands you one digit at a time — no % or // needed. int(d) changes each character back into a number.
program.py
n = int(input())
digits = str(n)
k = len(digits)
total = 0
for d in digits:
total += int(d) ** k
if total == n:
print(n, "is an Armstrong number")
else:
print(n, "is not an Armstrong number")Input
153
Output
153 is an Armstrong number
Print all Armstrong numbers between 100 and 999
The same digit loop, placed inside a for loop that tries every 3-digit number. Every number here has three digits, so the power is fixed at 3.
program.py
for n in range(100, 1000):
total = 0
temp = n
while temp > 0:
total += (temp % 10) ** 3
temp //= 10
if total == n:
print(n)Input
(no input)
Output
153 370 371 407
Questions students ask
- What is an Armstrong number in Python?
- It is a number that equals the sum of its own digits, each raised to the power of the number of digits. 153 has three digits and 1³ + 5³ + 3³ = 1 + 125 + 27 = 153, so 153 is an Armstrong number. In Python you check it by taking the digits out one by one with % 10 and // 10.
- Which numbers are Armstrong numbers?
- The 3-digit Armstrong numbers are 153, 370, 371 and 407. The 4-digit ones are 1634, 8208 and 9474. Every single-digit number is also an Armstrong number, because a digit raised to the power 1 is itself.
- What do 153 % 10 and 153 // 10 give in Python?
- 153 % 10 gives 3, the last digit. 153 // 10 gives 15, the number with its last digit removed. Repeating the two operations walks through every digit: 3, then 5, then 1.
- Is 1634 an Armstrong number?
- Yes. It has four digits, so each digit is raised to the power 4: 1 + 1296 + 81 + 256 = 1634. A program that always cubes the digits gets this one wrong.
Now solve a similar question yourself
A new Python question every time, checked instantly.
Preparing for NIELIT O Level?
This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.
