Write a Python Program to Find Armstrong Number in an Interval

In this tutorial, we will learn how to find Armstrong numbers within a given interval using Python.


An Armstrong number is a number whose sum of cubes of its digits is equal to the number itself.

For example, 153 is an Armstrong number because:

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

To solve this problem, we can follow these steps:

  1. Take the input from the user for the starting and ending numbers of the interval.
  2. Loop through the interval and check whether each number is an Armstrong number or not.
  3. Print the Armstrong numbers found in the interval.

Let’s write the Python code to achieve this.

# take input from the user for the interval
start = int(input("Enter the starting number: "))
end = int(input("Enter the ending number: "))

# loop through the interval and check for Armstrong numbers
for num in range(start, end+1):
    # find the sum of cubes of its digits
    sum = 0
    temp = num
    while temp > 0:
        digit = temp % 10
        sum += digit ** 3
        temp //= 10

    # check if the number is an Armstrong number
    if num == sum:
        print(num)

In the above code, we take the input from the user for the starting and ending numbers of the interval.

Then, we loop through the interval and check for each number whether it is an Armstrong number or not.

If a number is an Armstrong number, we print it.

In the loop, we find the sum of cubes of its digits by looping through each digit of the number and adding its cube to the sum.

We do this using a while loop.

Then, we check whether the number is an Armstrong number by comparing it with the sum we just calculated.


In conclusion, this is how we can find Armstrong numbers within a given interval using Python.