The Armstrong Number Program in Python is one of the most common beginner-level Python programming problems. It is frequently used in programming practice, coding interviews, lab examinations, and B.Tech practical examinations.
An Armstrong number is a number that is equal to the sum of its digits, where each digit is raised to the power of the total number of digits in the number.
For example, 153 is an Armstrong number because:
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Therefore, 153 is an Armstrong number.
In this tutorial, you will learn how to write an Armstrong Number Program in Python with output. We will also cover the Armstrong number logic, algorithm, flowchart concept, different Python approaches, examples, common mistakes, time complexity, and frequently asked questions.
What Is an Armstrong Number?
An Armstrong number is a number that is equal to the sum of its digits, with each digit raised to the power of the number of digits in the number.
For a number containing n digits:
abcd... = aⁿ + bⁿ + cⁿ + dⁿ + ...
For example, consider the number:
153
It contains 3 digits.
Therefore, each digit is raised to the power of 3:
1³ + 5³ + 3³
Calculating each value:
1³ = 1
5³ = 125
3³ = 27
Adding them:
1 + 125 + 27 = 153
Since the result is equal to the original number, 153 is an Armstrong number.
Examples of Armstrong Numbers
Some common Armstrong numbers are:
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748
92727
93084
For three-digit numbers, the commonly discussed Armstrong numbers are:
153, 370, 371, 407
Armstrong Number Formula
Suppose a number contains n digits.
The Armstrong condition is:
sum of (each digit)ⁿ = original number
For example, for 370:
3³ + 7³ + 0³
Calculate:
27 + 343 + 0
= 370
Therefore:
370 = Armstrong number
Armstrong Number Program in Python
The following is a simple Python program to check whether a given number is an Armstrong number.
Python Program
num = int(input("Enter a number: "))
original = num
digits = len(str(num))
total = 0
while num > 0:
digit = num % 10
total += digit ** digits
num //= 10
if total == original:
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
Example 1: Input
Enter a number: 153
Output
153 is an Armstrong number
Example 2: Input
Enter a number: 123
Output
123 is not an Armstrong number
How the Armstrong Number Program Works
Let’s understand the program step by step.
Step 1: Take Input
num = int(input("Enter a number: "))
The input() function accepts a number from the user.
The int() function converts the entered value into an integer.
For example:
Enter a number: 153
The variable contains:
num = 153
Step 2: Store the Original Number
original = num
We store the original number because the value of num will change while extracting its digits.
For example:
original = 153
num = 153
During the loop, num will eventually become zero.
Therefore, we need original to compare the final sum with the number entered by the user.
Step 3: Count the Number of Digits
digits = len(str(num))
Here:
str(num)
converts the number into a string.
For example:
str(153)
becomes:
"153"
The len() function counts the characters.
Therefore:
len("153") = 3
So:
digits = 3
Step 4: Initialize the Sum
total = 0
The variable total stores the sum of the powered digits.
Initially:
total = 0
Step 5: Extract Each Digit
The program uses:
while num > 0:
Inside the loop:
digit = num % 10
The % operator returns the remainder.
For example:
153 % 10 = 3
So the last digit is:
3
Step 6: Raise the Digit to the Required Power
total += digit ** digits
The ** operator is Python’s exponentiation operator.
For 153:
3³ = 27
The result is added to total.
Step 7: Remove the Last Digit
num //= 10
The // operator performs integer division.
For example:
153 // 10 = 15
The last digit has now been removed.
The process continues:
15 % 10 = 5
Then:
5 % 10 = 5
After all digits have been processed, num becomes zero.
Step 8: Compare the Result
Finally:
if total == original:
If the calculated sum is equal to the original number, it is an Armstrong number.
Otherwise, it is not.
Dry Run of Armstrong Number Program
Let’s perform a dry run using:
153
The number has:
3 digits
Therefore, every digit is raised to power 3.
First Iteration
num = 153
digit = 153 % 10
digit = 3
Calculate:
3³ = 27
So:
total = 27
Remove the last digit:
153 // 10 = 15
Second Iteration
num = 15
digit = 15 % 10
digit = 5
Calculate:
5³ = 125
Add:
27 + 125 = 152
Remove the last digit:
15 // 10 = 1
Third Iteration
num = 1
digit = 1 % 10
digit = 1
Calculate:
1³ = 1
Add:
152 + 1 = 153
Now:
num = 1 // 10
num = 0
The loop stops.
Finally:
total = 153
original = 153
Since both are equal:
153 is an Armstrong number
Armstrong Number Program Using a For Loop
Python can also use a for loop to process the digits.
One simple approach is to convert the number into a string.
Python Code
num = int(input("Enter a number: "))
digits = len(str(num))
total = 0
for digit in str(num):
total += int(digit) ** digits
if total == num:
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output
Enter a number: 153
153 is an Armstrong number
This method is easy to understand because the program directly goes through each digit.
Armstrong Number Using a Function
Using a function makes the program reusable.
Python Program
def is_armstrong(num):
digits = len(str(num))
total = 0
for digit in str(num):
total += int(digit) ** digits
return total == num
num = int(input("Enter a number: "))
if is_armstrong(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output
Enter a number: 371
371 is an Armstrong number
The function:
is_armstrong(num)
returns either:
True
or:
False
This makes the function useful in larger programs.
Armstrong Number Using Mathematical Operations
We can also avoid converting the number to a string.
This approach uses mathematical operators such as:
%//**
Python Program
num = int(input("Enter a number: "))
original = num
temp = num
digits = 0
while temp > 0:
digits += 1
temp //= 10
temp = num
total = 0
while temp > 0:
digit = temp % 10
total += digit ** digits
temp //= 10
if total == original:
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
Output
Enter a number: 370
370 is an Armstrong number
This version is useful when you want to understand how digit extraction works mathematically.
Armstrong Number Using String Method
A shorter solution can be written using strings.
num = input("Enter a number: ")
power = len(num)
total = sum(int(digit) ** power for digit in num)
if total == int(num):
print(num, "is an Armstrong number")
else:
print(num, "is not an Armstrong number")
Output
Enter a number: 9474
9474 is an Armstrong number
This approach uses a generator expression:
sum(int(digit) ** power for digit in num)
It is compact but beginners may find the traditional loop easier to understand.
Armstrong Numbers Between Two Numbers
Another common programming question is:
Write a Python program to print all Armstrong numbers between two given numbers.
Python Program
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))
print("Armstrong numbers are:")
for num in range(start, end + 1):
digits = len(str(num))
total = 0
for digit in str(num):
total += int(digit) ** digits
if total == num:
print(num, end=" ")
Example Input
Enter the starting number: 100
Enter the ending number: 500
Output
Armstrong numbers are:
153 370 371 407
This program checks every number in the specified range.
Armstrong Numbers From 1 to 1000
We can use a similar program to find Armstrong numbers between 1 and 1000.
print("Armstrong numbers from 1 to 1000:")
for num in range(1, 1001):
digits = len(str(num))
total = 0
for digit in str(num):
total += int(digit) ** digits
if total == num:
print(num, end=" ")
Output
Armstrong numbers from 1 to 1000:
1 2 3 4 5 6 7 8 9 153 370 371 407
Single-digit numbers are generally considered Armstrong numbers because:
5¹ = 5
Therefore, every single-digit non-negative integer satisfies the Armstrong condition.
Armstrong Number Program for Three-Digit Numbers
In some beginner exercises, the question specifically asks for a three-digit Armstrong number.
For a three-digit number, every digit is raised to power 3.
Python Program
num = int(input("Enter a three-digit number: "))
original = num
total = 0
while num > 0:
digit = num % 10
total += digit ** 3
num //= 10
if total == original:
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
Example
For:
153
the calculation is:
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Output:
153 is an Armstrong number
Armstrong Number Algorithm
The algorithm for checking an Armstrong number is straightforward.
Step 1
Start the program.
Step 2
Read a number from the user.
Step 3
Store the original number.
Step 4
Count the number of digits.
Step 5
Extract each digit.
Step 6
Raise each digit to the power of the total number of digits.
Step 7
Add all the calculated values.
Step 8
Compare the calculated sum with the original number.
Step 9
If both are equal, print that the number is an Armstrong number.
Step 10
Otherwise, print that the number is not an Armstrong number.
Step 11
Stop.
Pseudocode
START
Read number
Store number in original
Find number of digits
Set total = 0
WHILE number > 0
Extract last digit
Raise digit to the power of number of digits
Add result to total
Remove last digit
END WHILE
IF total equals original
Print "Armstrong number"
ELSE
Print "Not an Armstrong number"
END IF
STOP
Flowchart Logic
The basic flow of an Armstrong number program is:
START
|
Read Number
|
Store Original Number
|
Count Number of Digits
|
total = 0
|
Extract Last Digit
|
digit ^ number_of_digits
|
Add Result to total
|
Remove Last Digit
|
Are digits remaining?
/ \
Yes No
| |
└── Repeat |
|
total == original?
/ \
Yes No
| |
Armstrong Not Armstrong
\ /
STOP
Important Python Operators Used
The Armstrong number program uses several important Python operators.
Modulus Operator %
The modulus operator returns the remainder.
153 % 10
Output:
3
It is used to extract the last digit.
Floor Division //
Floor division removes the last digit when dividing by 10.
153 // 10
Output:
15
Exponentiation **
The exponentiation operator calculates powers.
5 ** 3
Output:
125
These three operators are especially important when solving number-based programming problems.
Difference Between Armstrong Number and Palindrome Number
Armstrong numbers and palindrome numbers are different concepts.
An Armstrong number is checked by calculating the sum of powered digits.
For example:
153
1³ + 5³ + 3³ = 153
A palindrome number remains the same when its digits are reversed.
For example:
121
Reverse:
121
Therefore, 121 is a palindrome.
However, 121 is not an Armstrong number because:
1³ + 2³ + 1³ = 10
which is not 121.
Common Armstrong Number Examples
Example 1: 153
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Therefore:
153 → Armstrong number
Example 2: 370
3³ + 7³ + 0³
= 27 + 343 + 0
= 370
Therefore:
370 → Armstrong number
Example 3: 371
3³ + 7³ + 1³
= 27 + 343 + 1
= 371
Therefore:
371 → Armstrong number
Example 4: 407
4³ + 0³ + 7³
= 64 + 0 + 343
= 407
Therefore:
407 → Armstrong number
Example 5: 123
1³ + 2³ + 3³
= 1 + 8 + 27
= 36
Since:
36 ≠ 123
123 is not an Armstrong number.
Armstrong Number Examples With More Than Three Digits
Armstrong numbers are not limited to three-digit numbers.
For example:
1634
contains four digits.
Therefore:
1⁴ + 6⁴ + 3⁴ + 4⁴
Calculate:
1 + 1296 + 81 + 256
= 1634
Therefore:
1634 is an Armstrong number.
Another example is:
9474
It is also an Armstrong number because:
9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474
This demonstrates why the program should use the number of digits dynamically rather than always using power 3.
Time Complexity
Suppose the input number contains D digits.
The program processes each digit once.
Therefore, the time complexity is approximately:
O(D)
For normal integer inputs, D is relatively small.
The extra space used by the basic algorithm is:
O(1)
because only a few variables are required.
Common Mistakes in Armstrong Number Programs
Mistake 1: Forgetting the Original Number
If you modify the input number while extracting digits and then compare the result with the modified value, the program will not work correctly.
Use:
original = num
before modifying num.
Mistake 2: Always Using Power 3
This works only for three-digit Armstrong numbers.
For example:
digit ** 3
is not sufficient for a general Armstrong number program.
Instead, calculate:
digits = len(str(num))
and use:
digit ** digits
Mistake 3: Using / Instead of //
The following:
num / 10
produces a floating-point result.
For digit extraction, use:
num // 10
Mistake 4: Incorrect Digit Extraction
The last digit should be obtained using:
digit = num % 10
For example:
153 % 10 = 3
Mistake 5: Incorrect Comparison
At the end, compare:
if total == original:
not with the modified num.
Armstrong Number Program for B.Tech Practical Exam
For a B.Tech Python practical examination, the following version is simple and easy to explain.
Aim
To write a Python program to check whether a given number is an Armstrong number.
Program
num = int(input("Enter a number: "))
original = num
digits = len(str(num))
total = 0
while num > 0:
digit = num % 10
total += digit ** digits
num //= 10
if total == original:
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
Sample Input
Enter a number: 153
Sample Output
153 is an Armstrong number
Result
Thus, the Python program successfully checks whether the given number is an Armstrong number.
Why Learn Armstrong Number Programs?
The Armstrong number problem may look like a simple mathematical exercise, but it teaches several important programming concepts.
By solving this problem, students learn how to:
- Take input from users.
- Store values in variables.
- Use loops.
- Extract digits from numbers.
- Use arithmetic operators.
- Calculate powers.
- Compare values.
- Create reusable functions.
- Understand algorithms.
- Analyze time complexity.
These concepts are useful in many other programming problems.
Frequently Asked Questions
What is an Armstrong number?
An Armstrong number is a number that is equal to the sum of its digits raised to the power of the total number of digits.
For example:
153 = 1³ + 5³ + 3³
Therefore, 153 is an Armstrong number.
What is the simplest Armstrong number program in Python?
A simple program is:
num = int(input("Enter a number: "))
original = num
digits = len(str(num))
total = 0
while num > 0:
digit = num % 10
total += digit ** digits
num //= 10
if total == original:
print("Armstrong number")
else:
print("Not an Armstrong number")
Is 153 an Armstrong number?
Yes. 153 is an Armstrong number because:
1³ + 5³ + 3³ = 153
Is 370 an Armstrong number?
Yes.
3³ + 7³ + 0³ = 370
Therefore, 370 is an Armstrong number.
Is 371 an Armstrong number?
Yes.
3³ + 7³ + 1³ = 371
Therefore, 371 is an Armstrong number.
Is 407 an Armstrong number?
Yes.
4³ + 0³ + 7³ = 407
Therefore, 407 is an Armstrong number.
What are the Armstrong numbers between 100 and 500?
The Armstrong numbers between 100 and 500 are:
153, 370, 371, 407
Can we check Armstrong numbers using a for loop?
Yes. Python’s for loop can iterate through the digits of a number.
for digit in str(num):
total += int(digit) ** digits
Can we use a function to check Armstrong numbers?
Yes. Creating a function is useful when the Armstrong check needs to be performed multiple times.
What is the time complexity of the Armstrong number program?
If the number contains D digits, the algorithm takes approximately:
O(D)
time.
Can Armstrong numbers have more than three digits?
Yes. Armstrong numbers can have any number of digits.
For example:
1634
8208
9474
are Armstrong numbers.
Is every single-digit number an Armstrong number?
Yes, under the standard definition, every single-digit non-negative integer is an Armstrong number because raising a digit to the first power gives the same digit.
For example:
7¹ = 7
Conclusion
The Armstrong Number Program in Python is an important beginner-level programming problem that teaches students how to work with digits, loops, arithmetic operators, powers, and conditional statements.
The basic logic is simple:
- Read the number.
- Count its digits.
- Extract each digit.
- Raise each digit to the power of the number of digits.
- Add the results.
- Compare the sum with the original number.
- If both values are equal, the number is an Armstrong number.
For example, for 153:
1³ + 5³ + 3³
= 1 + 125 + 27
= 153
Therefore:
153 is an Armstrong number.
A good general-purpose Python implementation is:
num = int(input("Enter a number: "))
original = num
digits = len(str(num))
total = 0
while num > 0:
digit = num % 10
total += digit ** digits
num //= 10
if total == original:
print(original, "is an Armstrong number")
else:
print(original, "is not an Armstrong number")
Understanding this program gives beginners a strong foundation for solving other number-based Python problems such as Palindrome Number, Perfect Number, Prime Number, Reverse a Number, Factorial, Fibonacci Series, and Sum of Digits.
For B.Tech students preparing for Python practical examinations, it is useful to learn the program, algorithm, dry run, sample input, sample output, and explanation rather than memorizing only the code.




