Python Program to Check Armstrong Number
An Armstrong number is a special number in mathematics where the sum of the digits raised to the power of the number of digits is equal to the number itself. This is a popular programming problem that helps beginners understand loops, mathematical operations, and number manipulation. In this tutorial, we will explore different ways to check Armstrong numbers in Python.
What is an Armstrong Number?
An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its own digits each raised to the power of the number of digits. Examples: - 153 = 1³ + 5³ + 3³ = 153 - 9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴ = 9474
Why Learn This Program?
This program helps you understand loops, exponentiation, and digit extraction. It is commonly used in coding practice and interviews.
Step-by-Step Algorithm
- Start the program
- Input a number
- Count number of digits
- Initialize sum to 0
- Extract each digit
- Raise digit to power of digit count
- Add to sum
- Compare sum with original number
- Display result
- End program
Python Program Using Loop
# Armstrong number check
num = int(input("Enter a number: "))
original = num
power = len(str(num))
sum_val = 0
while num != 0:
digit = num % 10
sum_val += digit ** power
num //= 10
if sum_val == original:
print("Armstrong Number")
else:
print("Not an Armstrong Number")
Sample Output: Enter a number: 153 Armstrong Number
Code Explanation
We first count the number of digits in the number. Then we extract each digit, raise it to the power of the digit count, and add it to a running total. Finally, we compare the result with the original number.
Using String Method
# Using string method
num = input("Enter a number: ")
power = len(num)
sum_val = sum(int(digit) ** power for digit in num)
if int(num) == sum_val:
print("Armstrong Number")
else:
print("Not Armstrong")
Using Function
def is_armstrong(n):
power = len(str(n))
return n == sum(int(d) ** power for d in str(n))
num = int(input("Enter a number: "))
if is_armstrong(num):
print("Armstrong Number")
else:
print("Not Armstrong")
Finding Armstrong Numbers in Range
# Armstrong numbers in range
start = int(input("Start: "))
end = int(input("End: "))
for num in range(start, end + 1):
power = len(str(num))
if num == sum(int(d) ** power for d in str(num)):
print(num, end=" ")
Real-World Applications
- Mathematical analysis
- Pattern recognition
- Algorithm practice
- Educational tools
- Coding competitions
Common Mistakes to Avoid
- Incorrect power calculation
- Forgetting to reset variables
- Mixing string and integer types
- Ignoring negative numbers
- Wrong loop condition
Advanced Enhancements
- Find Armstrong numbers for large ranges
- Optimize power calculation
- Build number analyzer tool
- Create GUI-based checker
- Integrate into web apps
Practice Exercises
- Find Armstrong numbers between 1 and 1000
- Count Armstrong numbers in range
- Check multi-digit Armstrong numbers
- Combine with palindrome checker
- Build number analysis system
Conclusion
The Armstrong number program is a great exercise for understanding loops and mathematical operations. By practicing different methods, you can strengthen your programming skills and apply these concepts to more complex problems.
Codecrown