0
0
PythonProgramBeginner · 2 min read

Python Program to Find Armstrong Number

A Python program to find an Armstrong number checks if the sum of each digit raised to the power of the number of digits equals the number itself, for example: sum(int(digit) ** len(str(num)) for digit 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 find if a number is an Armstrong number, first count 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
Convert the number to a string to count digits.
3
Calculate the number of digits.
4
For each digit, raise it to the power of the number of digits and sum these values.
5
Compare the sum with the original number.
6
If equal, print it is an Armstrong number; otherwise, print it is not.
💻

Code

python
num = int(input('Enter a number: '))
digits = str(num)
power = len(digits)
sum_of_powers = sum(int(d) ** power for d in digits)
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 and convert to string

num = 153, digits = '153'

2

Count digits

power = 3

3

Calculate 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^PowerRunning Sum
11^3 = 11
55^3 = 125126
33^3 = 27153
💡

Why This Works

Step 1: Counting digits

We convert the number to a string to easily count how many digits it has using len().

Step 2: Raising digits to power

Each digit is raised to the power of the total digits because Armstrong numbers are defined this way.

Step 3: Comparing sum to original

If the sum of these powered digits equals the original number, it confirms the number is Armstrong.

🔄

Alternative Approaches

Using while loop and math.pow
python
num = int(input('Enter a number: '))
original = num
power = len(str(num))
sum_of_powers = 0
while num > 0:
    digit = num % 10
    sum_of_powers += int(pow(digit, power))
    num //= 10
if sum_of_powers == original:
    print(f'{original} is an Armstrong number')
else:
    print(f'{original} is not an Armstrong number')
This approach uses a loop and math.pow instead of string iteration; it is more manual but avoids string conversion for digits.
Using list comprehension and map
python
num = int(input('Enter a number: '))
power = len(str(num))
digits = list(map(int, str(num)))
sum_of_powers = sum(d ** power for d in digits)
print(f'{num} is an Armstrong number' if sum_of_powers == num else f'{num} is not an Armstrong number')
This uses map to convert digits to integers explicitly, which some find clearer.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through each digit once, so time grows linearly with the number of digits, O(n).

Space Complexity

Converting the number to a string and storing digits uses O(n) space, where n is the number of digits.

Which Approach is Fastest?

All approaches have similar time complexity; using string iteration is simpler and more readable, while the loop with math.pow is more manual but avoids string overhead.

ApproachTimeSpaceBest For
String iteration with sum and comprehensionO(n)O(n)Simplicity and readability
While loop with math.powO(n)O(1)Manual control, no string conversion
List comprehension with mapO(n)O(n)Explicit digit conversion, clear code
💡
Use string conversion to easily access each digit when checking Armstrong numbers.
⚠️
Forgetting to raise each digit to the power of the total number of digits instead of a fixed power like 3.