# Python Program to Find Armstrong Number in an Interval [4 Examples]

Are you struggling to find the Armstrong number in an Interval in Python? Let me explain to you with various examples.

If the sum of the digits is raised to the power of the length is equal to the number itself, It’s an Armstrong number. So, we can create a custom function using loops and conditional statements to find an Armstrong number in Python.

Keep reading this Python article to learn how to write a Python Program to Find Armstrong Number in an Interval. I will explain the below topics in this article.

• Armstrong number between 1 to 1000
• Armstrong number in a range
• By importing math
• Print first n Armstrong numbers

Let’s begin to explore Python for finding Armstrong numbers in an interval.

## Armstrong number in Python between 1 to 1000

Here is the full code of Python to find the Armstrong number between 1 to 1000.

``````low_range = 1
high_range = 1000

for num in range(low_range, high_range + 1):
order = len(str(num))
armstrong_sum = 0

temp = num
while temp > 0:
digit = temp % 10
armstrong_sum += digit ** order
temp //= 10

if num == armstrong_sum:
print(num, end=" , ")``````

Here, the purpose of using these lines of Python code is to iterate over a range of the given numbers. Then, calculate the sum of each digit raised to the power of the number of digits in the given number.

Now, check if the sum equals the original number. This process is a common and easy way to check for Armstrong numbers.

``````for num in range(low_range, high_range + 1):
order = len(str(num))
armstrong_sum = 0

temp = num
while temp > 0:
digit = temp % 10
armstrong_sum += digit ** order
temp //= 10``````

Output:

``1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 153 , 370 , 371 , 407 , ``

The screenshot below provides the result of the Python code to find the Armstrong number in Python between 1 and 1000.

## Python Program to Find Armstrong Numbers in a Range

We can ask the user to enter the lower and upper numbers as required to find the Armstrong number in a range in Python.

This is the complete Python program to find the Armstrong number in a range in Python.

``````lower_number = int(input("Enter lower number : "))
upper_number = int(input("Enter upper number : "))

for number in range(lower_number,upper_number + 1):
armstrong_sum = 0
temp = number
while temp > 0:
digit = temp % 10
armstrong_sum = armstrong_sum + digit ** 3
temp = temp // 10

if number == armstrong_sum:
print(number)``````

Ask the user to enter the lower and upper numbers to set a range and find the Armstrong number in Python in that range.

``````lower_number = int(input("Enter lower number : "))
upper_number = int(input("Enter upper number : "))  ``````

Output:

``Enter lower number : 100Enter upper number : 500153370371407``

You can refer to the image below, which provides the result of the Python code to find the Armstrong number in a range in Python.

## Python Program to Find Armstrong Number in an Interval by importing math function

Python has a set of built-in math functions that extends the list of mathematical functions and allows you to perform mathematical tasks on numbers, i.e., The math module.

We can use the math.pow() function to calculate the power of the numbers.

Here is the complete source code of Python to find Armstrong numbers using the math module.

``````import math

first, second = 150, 10000

def is_Armstrong(val):
armstrong_sum_value  = 0
temp_list = [int(d) for d in str(val)]

for i in range(0, len(temp_list)):
armstrong_sum_value = armstrong_sum_value + math.pow(temp_list[i], len(temp_list))

if   armstrong_sum_value  == val:
print(str(val) + ", ", end="")

for i in range(first, second + 1):
is_Armstrong(i)``````

Here, we are likely to sum the current value of the Armstrong number to the current digit raised to the power of the total number of digits in temp_list. This math.pow() function from the math module is used to compute the power of a given number.

`` armstrong_sum_value = armstrong_sum_value + math.pow(temp_list[i], len(temp_list))``

Output:

``153, 370, 371, 407, 1634, 8208, 9474,``

Refer to the image below, it provides the result of the Python program to find the Armstrong number using a math function.

## Python Program to Print the First “N” Armstrong Number

This is another way to find the Armstrong number in Python. Here is the complete Python code for getting the first N Armstrong in Python.

``````limit_for_armstrong_number = int(input('For getting the first N Armstrong numbers, enter the value of N: '))
count = 0
initial_value = 1
print('The Armstrong numbers in this range are: ')

while(count!=limit_for_armstrong_number):
num_digits=len(str(initial_value))
current_num=initial_value
digit_sum = 0
if(num_digits>3):
while current_num!= 0:
digit = num_digits%10
digit_sum += digit** num_digits
current_num = current_num//10
if digit_sum == initial_value:
count+=1
print(initial_value)
else:
while current_num!= 0:
digit=current_num%10
digit_sum += digit**3
current_num = current_num//10
if digit_sum == initial_value:
count+=1
print(initial_value)
initial_value+=1
``````

Here, start the while loop until the desired Armstrong number is found and calculate the number of digits in the current initial value_value. Then, set the current_num to the current number being evaluated and initialize the variable value to 0.

``while(count!=limit_for_armstrong_number):num_digits=len(str(initial_value))current_num=initial_valuedigit_sum = 0``

Below while loop in Python checks if the number of digits exceeds 3. It iterates through each digit until it becomes 0 and then extracts the sum of its cube. Extract the last digit of the number and store it in the value of the digit (i.e., n).

Now remove the last digit using the // operator in Python and check if the sum of its cube equals the original number then increment by +1 if it is an Armstrong number.

``````if(num_digits>3):
while current_num!= 0:
digit = num_digits%10
digit_sum += digit** num_digits
current_num = current_num//10
if digit_sum == initial_value:
count+=1
print(initial_value)``````

Inside this below while loop, we extract the last digit of the number and compute the sum of its cube. Then remove the last digit from the number and check if the condition is True. The count is incremented.

Also, it increments the current number for the next iteration.

``````while current_num!= 0:
digit=current_num%10
digit_sum += digit**3
current_num = current_num//10
if digit_sum == initial_value:
count+=1
print(initial_value)
initial_value+=1``````

Output: The screenshot below provides the result after executing the Python program.

## Conclusion

I hope this tutorial provides useful information for Python program to find Armstrong number in an interval. Here, I have demonstrated various examples that help you to find the Armstrong number in Python in different scenarios such as between 1 to 1000, in a given range, using a math function, and to find the first N Armstrong number.

I hope you understand all the examples.

You may also like to read: