# Python Program to Check Armstrong Number

The Armstrong number is a fascinating concept that intrigues mathematicians and programmers alike. Known for its unique mathematical properties, the Armstrong number serves as an exciting subject for coding exercises, competitive programming, and algorithm analysis. This comprehensive article aims to delve deep into the world of Armstrong numbers and elucidate how to build a Python program to identify such numbers.

1. Introduction to Armstrong Numbers
2. Mathematical Explanation
3. Basic Python Program to Check Armstrong Number
4. Implementing Input Validation
5. Using Functions
6. Performance Considerations
7. Conclusion

## 1. Introduction to Armstrong Numbers

An Armstrong number (also known as a narcissistic number) is a number that is equal to the sum of its digits, each raised to the power of the number of digits in the original number. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 =153

## 2. Mathematical Explanation

Mathematically, an n-digit number m is an Armstrong number if:

where d1,d2,…,dn​ are the digits of m.

## 3. Basic Python Program to Check Armstrong Number

Here’s a straightforward Python program to check if a number is an Armstrong number:

num = int(input("Enter a number: "))
temp = num
sum = 0
n = len(str(num))

while temp > 0:
digit = temp % 10
sum += digit ** n
temp //= 10

if num == sum:
print(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")

## 4. Implementing Input Validation

Input validation is crucial for any program that interacts with the user. Here’s how you can validate the input:

try:
num = int(input("Enter a number: "))
except ValueError:
exit(1)

## 5. Using Functions

Breaking down your code into functions enhances readability and reusability. Here’s the same program, but with a function:

def is_armstrong(num):
temp = num
sum = 0
n = len(str(num))

while temp > 0:
digit = temp % 10
sum += digit ** n
temp //= 10

return num == sum

num = int(input("Enter a number: "))
print(f"{num} is an Armstrong number." if is_armstrong(num) else f"{num} is not an Armstrong number.")

## 6. Performance Considerations

For numbers with a large number of digits, computational complexity becomes a concern. The basic approach has a time complexity of O(n), which is reasonably efficient.

## 7. Conclusion

Armstrong numbers offer a delightful mix of mathematics and programming. Writing a Python program to identify these numbers serves as an excellent exercise in understanding loops, conditional statements, and functions. This article has aimed to provide a comprehensive guide to understanding Armstrong numbers and writing a Python program to identify them.