0
0
PythonProgramBeginner · 2 min read

Python Program to Check Armstrong Number

To check if a number is an Armstrong number in Python, calculate the sum of each digit raised to the power of the number of digits and compare it with the original number using code like sum(int(d)**len(str(num)) for d in str(num)) == num.
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is an Armstrong number, first find how many digits it has. Then, for each digit, raise it to the power of the total digits and add all these values together. If this sum equals the original number, it is an Armstrong number.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
For each digit in the number, raise it to the power of the digit count and sum these values.
4
Compare the sum with the original number.
5
If they are equal, the number is an Armstrong number; otherwise, it is not.
💻

Code

python
num = int(input("Enter a number: "))
digits = len(str(num))
sum_of_powers = sum(int(d) ** digits for d in str(num))
if sum_of_powers == num:
    print(f"{num} is an Armstrong number")
else:
    print(f"{num} is not an Armstrong number")
Output
Enter a number: 153 153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code

1

Input number

num = 153

2

Count digits

digits = 3 because '153' has 3 characters

3

Calculate sum of powers

sum_of_powers = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

4

Compare sum with original

153 == 153 is True

5

Print result

"153 is an Armstrong number"

DigitDigit^3Running Sum
111
5125126
327153
💡

Why This Works

Step 1: Count digits

We use len(str(num)) to find how many digits the number has, which is needed for the power calculation.

Step 2: Sum of powers

Each digit is raised to the power of the digit count and added together using a generator expression inside sum().

Step 3: Comparison

If the sum equals the original number, it means the number is an Armstrong number, so we print the positive result.

🔄

Alternative Approaches

Using while loop
python
num = int(input("Enter a number: "))
temp = num
digits = len(str(num))
sum_of_powers = 0
while temp > 0:
    digit = temp % 10
    sum_of_powers += digit ** digits
    temp //= 10
if sum_of_powers == num:
    print(f"{num} is an Armstrong number")
else:
    print(f"{num} is not an Armstrong number")
This approach uses a loop and arithmetic operations instead of string conversion, which some may find clearer or more traditional.
Using recursion
python
def armstrong_sum(num, digits):
    if num == 0:
        return 0
    return (num % 10) ** digits + armstrong_sum(num // 10, digits)
num = int(input("Enter a number: "))
digits = len(str(num))
sum_of_powers = armstrong_sum(num, digits)
if sum_of_powers == num:
    print(f"{num} is an Armstrong number")
else:
    print(f"{num} is not an Armstrong number")
This recursive method breaks down the number digit by digit, which is elegant but less efficient for very large numbers.

Complexity: O(d) time, O(1) space

Time Complexity

The program loops through each digit once, so the time depends linearly on the number of digits, which is O(d).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The string-based approach is concise and fast for typical inputs, while the loop method avoids string conversion but is similar in speed; recursion is less efficient due to function call overhead.

ApproachTimeSpaceBest For
String with sum()O(d)O(1)Simplicity and readability
While loop with arithmeticO(d)O(1)Traditional numeric processing
RecursionO(d)O(d)Learning recursion, less efficient
💡
Convert the number to a string to easily iterate over each digit for power calculations.
⚠️
Forgetting to raise each digit to the power of the total number of digits instead of a fixed power like 3.