Armstrong Number Program in Python
If you are diving into the world of programming, you’ve likely encountered "special numbers." Among the most famous is the Armstrong number. Whether you are preparing for a technical interview or practicing your logic in Python, understanding how to identify these numbers is a rite of passage.
In this guide, we will break down the Armstrong number program in python using various methodsfrom while loops to for loopsand even a quick nod to how it compares to C.
What Exactly is an Armstrong Number?
An Armstrong number (also known as a narcissistic number) of order n is a number that is equal to the sum of its own digits each raised to the power of n.
Armstrong number in Python 153 example:
For the number 153, the number of digits is 3.
Calculation: $1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153$.
Since the sum equals the original number, 153 is an Armstrong number.
1. Armstrong Number Program in Python Using While Loop
The most common way to solve this is by using a while loop. This method is highly efficient as it allows us to peel off each digit one by one using the modulo operator.
# Armstrong number Program in Python without using function
num = int(input("Enter a number: "))
order = len(str(num))
temp = num
sum_val = 0
while temp > 0:
digit = temp % 10
sum_val += digit ** order
temp //= 10
if num == sum_val:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
2. Armstrong Number Program in Python Using For Loop
Many learners prefer the Armstrong number program in python for loop approach because it leverages Python's ability to iterate directly over strings. This is often seen as the more "Pythonic" way to write it.
# Armstrong number program in python example using string iteration
num = input("Enter a number: ")
order = len(num)
sum_val = 0
for digit in num:
sum_val += int(digit) ** order
if int(num) == sum_val:
print(f"{num} is an Armstrong number")
else:
print(f"{num} is not an Armstrong number")
Python vs. C
While we are focusing on Python, you might be curious about how this looks in other languages. If you were asked to write a program to check whether a number is Armstrong or not in C, the logic remains the same, but you would have to manually calculate the number of digits and handle integer division more strictly.
Python makes this significantly easier by allowing us to convert the number to a string to find the "order" (the number of digits) instantly using len().
Quick Tips for Your Code
- Handle the Order: Always calculate the power based on the number of digits (e.g., for 1634, the power is 4).
- Input Validation: Ensure the user enters a positive integer to avoid runtime errors.
- Efficiency: For very large numbers, using mathematical operations (modulo) is generally faster than string conversion.
Frequently Asked Questions
Is 0 an Armstrong number?
Yes, $0^1 = 0$, so it fits the definition.
What are the Armstrong numbers between 1 and 1000?
The Armstrong numbers in this range are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, and 407.

Comments
Post a Comment