# Python Program to Find Armstrong Numbers in an Interval

Welcome to our site, In this article, We will explore more about Armstrong numbers and how to find them using Python programming. Armstrong numbers is a concept in mathematics, named after Michael F. Armstrong, which are also known as narcissistic numbers. In this article, we will discuss the definition of Armstrong numbers, and how to find Armstrong Numbers in an Interval.

**What Are Armstrong Numbers?**

Armstrong numbers also known as narcissistic numbers, are numbers that are equal to the sum of their own digits raised to the power of the number of digits in the number. For example, the number 153 is an Armstrong number because:

```
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
```

Other examples of Armstrong numbers include 370, 371, 407, 1634, and so on.

**How to Check if a Number Is an Armstrong Number?**

To check if a number is an Armstrong number, we need to find the number of digits in the number and then calculate the sum of the cubes of the digits. If the sum is equal to the number itself, then the number is an **Armstrong number.**

Here is the step-by-step guide to checking if a number is an Armstrong number

**Step 1**: Initially, We will start the program by finding the number of digits in the given input number. This can be done by converting the number to a string and then finding the length of the string (Which can be achieved using **len()** function).

**Step 2**: Initialize a variable `sum`

to 0.

**Step 3**: Extract each digit from the number and add its cube to `sum`

.

**Step 4**: Divide the number by 10 and repeat steps 3-4 until the number becomes 0.

**Step 5**: If `sum`

is equal to the original number, then the number is an Armstrong number. Otherwise, it’s not.

## Python Program to Find Armstrong Numbers in an Interval

Here is the Python program that finds Armstrong numbers in a given interval:

**# Custom function to check if a number is an Armstrong number**
def is_armstrong(n):
**# find the number of digits in n**
num_digits = len(str(n))
**# calculate the sum of the cubes of the digits**
sum = 0
temp = n
while temp > 0:
digit = temp % 10
sum += digit ** num_digits
temp //= 10
**# return True if the sum is equal to the number itself, False otherwise**
return sum == n
**# get the interval from the user**
lower = int(input("Enter lower bound of the interval: "))
upper = int(input("Enter upper bound of the interval: "))
**# find and print all Armstrong numbers in the interval**
print("Armstrong numbers in the interval [", lower, ",", upper, "]:")
for i in range(lower, upper+1):
if is_armstrong(i):
print(i)

In the above code, we define a function

that takes a number **is_armstrong(n)**

as input and returns **n**`True`

if `n`

is an Armstrong number, and `False`

otherwise.

while initiating the program it will prompt the user to enter the lower and upper bounds of the interval for which we want to find Armstrong numbers. We then use a

loop to iterate through all the numbers in the interval, and for each number/iteration, we use the **for**

function to check if it’s an Armstrong number or not. If yes we will print the output in the console. (Shown below)**is_armstrong**()

## Example Output

Suppose we want to find all Armstrong numbers between 99 and 999. We can run the Python program above and enter `99`

as the lower bound and `999`

as the upper bound when prompted. The output should be:

```
Enter lower bound of the interval: 99
Enter upper bound of the interval: 999
Armstrong numbers in the interval [ 99 , 999 ]:
153
370
371
407
``````
```

These are the three Armstrong numbers between 99 and 999.

**Note: **There are different approaches to achieving the same result of finding Armstrong numbers, Do comment if you have any other effective way to achieve the same results ðŸ™‚

## Conclusion

In this article, We have learned what Armstrong numbers are, how to check if a number is an Armstrong number, and how to create a Python program that finds Armstrong numbers in a given interval. Armstrong numbers are an interesting concept in mathematics and programming and don’t have any practical applications. Having said that, these exercises are often used as a fun way to practice programming skills, especially for beginners learning programming concepts like loops, conditionals, and functions etc.

Good Luck with your Learning !!

**Related Topics:**

ssl module in Python is not available

Python was not found; run without arguments

* Coding Spell | Python Journey* |

*About Us*- How to Fix – TypeError: only size-1 arrays can be converted to Python scalars - 16 October 2023
- How to Implement d’wave qbsolv in Python - 16 October 2023
- Resolve Javascript error: ipython is not defined - 15 October 2023