If you are learning Python, one of the most common beginner programs you will come across is the Armstrong number in Python. It is a simple number-based problem that helps you understand loops, conditions, digits, and basic logic building.
Many students and beginners practice this program for coding interviews, school assignments, and Python fundamentals. In this guide, you will learn what an Armstrong number is, how it works, and how to write a Python program to check it with easy examples.
What is an Armstrong Number?
An Armstrong number is a number that is equal to the sum of its own digits, where each digit is raised to the power of the total number of digits in that number.
In simple words, you break the number into digits, raise each digit to the number of digits, and then add them together. If the result is the same as the original number, it is called an Armstrong number.
Example:
Take the number 153
- It has 3 digits
- So we raise each digit to the power of 3
13+53+33=1531^3 + 5^3 + 3^3 = 15313+53+33=153
Since the result is equal to the original number 153, it is an Armstrong number.
Armstrong Number Examples
Here are some common examples to help you understand Armstrong numbers better:
| Number | Calculation | Result |
| 153 | 1³ + 5³ + 3³ = 153 | Armstrong |
| 370 | 3³ + 7³ + 0³ = 370 | Armstrong |
| 371 | 3³ + 7³ + 1³ = 371 | Armstrong |
| 407 | 4³ + 0³ + 7³ = 407 | Armstrong |
| 123 | 1³ + 2³ + 3³ = 36 | Not Armstrong |
These examples show that when the sum of the powered digits matches the original number, the number is called an Armstrong number.
Logic Behind Armstrong Number in Python
The logic of an Armstrong number program is simple and easy to understand.
Steps:
- Take a number as input.
- Count the total number of digits in the number.
- Separate each digit one by one.
- Raise each digit to the power of total digits.
- Add all the values together.
- Compare the final sum with the original number.
If both values are the same, the number is an Armstrong number.
Python Program to Check Armstrong Number
Now that you understand the logic, let’s write a simple Python program to check whether a number is an Armstrong number or not.
num = int(input("Enter a number: "))
temp = num
digits = len(str(num))
total = 0
while temp > 0:
digit = temp % 10
total += digit ** digits
temp //= 10
if total == num:
print("Armstrong Number")
else:
print("Not Armstrong Number")
This program takes a number as input, calculates the sum of each digit raised to the power of total digits, and then compares the result with the original number. If both are equal, it prints Armstrong Number.
Output Example
Here is how the program works when you run it with different inputs:
Enter a number: 153
Armstrong Number
Enter a number: 125
Not Armstrong Number
Armstrong Numbers Between 1 to 1000 in Python
You can also use Python to find all Armstrong numbers between 1 and 1000. This is a great way to understand loops and number logic.
for num in range(1, 1001):
power = len(str(num))
total = sum(int(digit) ** power for digit in str(num))
if total == num:
print(num)
Expected Output:
1
2
3
4
5
6
7
8
9
153
370
371
407
These are the Armstrong numbers between 1 and 1000.
Using Python Function
Instead of writing the same logic again and again, you can create a Python function to check whether a number is an Armstrong number.
def is_armstrong(num):
power = len(str(num))
total = sum(int(digit) ** power for digit in str(num))
return total == num
print(is_armstrong(153))
Output:
True
This function returns True if the number is an Armstrong number, otherwise it returns False.
Common Mistakes Beginners Make
When writing an Armstrong number program in Python, beginners often make these common mistakes:
- Using fixed cube power only – Armstrong numbers do not always use power 3. The power depends on the total number of digits.
- Forgetting total digits count – You must first count how many digits the number has.
- Changing original number directly – If you modify the original number, comparison at the end becomes difficult. Use a temporary variable instead.
- Wrong loop condition – Incorrect loop logic may skip digits or create an infinite loop.
Why Armstrong Number is Asked in Python?
Armstrong number is a common beginner-level Python program because it helps you practice important programming concepts.
It helps you learn:
- Loops – to go through digits one by one.
- Conditions – to compare the final result with the original number.
- Modulus operator (%) – to extract the last digit of a number.
- String conversion – to count digits easily using str().
- Logic building – to improve problem-solving skills with numbers.
That’s why Armstrong number programs are often used in coding practice, interviews, and academic assignments.
FAQs
Q1. Is 153 an Armstrong number?
Yes, 153 is an Armstrong number because it has 3 digits and the sum of each digit raised to the power of 3 equals the same number.
13+53+33=1531^3 + 5^3 + 3^3 = 15313+53+33=153
So, 153 is an Armstrong number.
Q2. Is 123 an Armstrong number?
No, 123 is not an Armstrong number because the sum of its digits raised to the power of 3 is not equal to 123.
13+23+33=361^3 + 2^3 + 3^3 = 3613+23+33=36
Since the result is 36, not 123, it is not an Armstrong number.
Q3. Are single digit numbers Armstrong?
Yes, single digit numbers like 1, 2, 3, 4 are usually considered Armstrong numbers because any number raised to the power of 1 remains the same.
Example:
51=55^1 = 551=5
Q4. Is Armstrong number asked in interviews?
Yes, Armstrong number programs are commonly asked in beginner-level coding interviews, school tests, and programming practice rounds because they check your understanding of loops, conditions, and number logic.
Conclusion
Armstrong number in Python is a great beginner coding problem that helps you understand loops, conditions, digits, and mathematical logic in a practical way. It is one of the best exercises for improving problem-solving skills and building confidence in Python programming.
Once you understand how Armstrong numbers work, many other number-based Python programs become much easier to learn and solve.