# Python Program to Find Armstrong Number in an Interval

Armstrong numbers, also known as narcissistic numbers, hold a special place in the realm of mathematics and computer science. These numbers are not only fascinating from a mathematical standpoint but are also popular subjects in programming tutorials and coding challenges. This article aims to provide an extensive guide on how to write a Python program to find Armstrong numbers within a given interval.

1. What is an Armstrong Number?
2. Mathematical Background
3. Understanding Intervals
4. A Basic Python Program to Find Armstrong Numbers in an Interval
5. Input Validation in Python
6. Employing Functions for Code Reusability
7. Time Complexity and Performance Metrics
8. Conclusion

## 1. What is an Armstrong Number?

An Armstrong number is an n-digit number that is equal to the sum of its digits, each raised to the power of n. For instance, 153 is an Armstrong number as 1^3 + 5^3 + 3^3=153.

## 2. Mathematical Background

An n-digit number m can be expressed as an Armstrong number if:

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

## 3. Understanding Intervals

An interval is simply a range of numbers. In this context, finding Armstrong numbers in an interval means identifying all Armstrong numbers that fall within a specific range, say between a and b, inclusive.

## 4. A Basic Python Program to Find Armstrong Numbers in an Interval

Here’s a straightforward Python program that finds Armstrong numbers in a given interval.

lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

for num in range(lower, upper + 1):
temp = num
sum = 0
n = len(str(num))

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

if num == sum:
print(num)

## 5. Input Validation in Python

It is crucial to validate user input to ensure the program’s robustness. Here’s how you can add input validation to the previous program:

try:
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
except ValueError:
exit(1)

## 6. Employing Functions for Code Reusability

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

def find_armstrong_in_interval(lower, upper):
for num in range(lower, upper + 1):
if is_armstrong(num):
print(num)

# Example usage
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
find_armstrong_in_interval(lower, upper)

## 7. Time Complexity and Performance Metrics

The primary algorithm iterates through each number in the interval and checks for its Armstrong nature, which gives it a time complexity of O(n).

## 8. Conclusion

This article has aimed to provide an exhaustive guide to writing a Python program that identifies Armstrong numbers in a given interval. We explored a basic approach, input validation, functional decomposition for better code reusability.

The identification of Armstrong numbers in an interval serves not just as an exciting programming exercise but also as a lesson in important programming concepts like loops, conditionals, and functions.