Italy's daily coronavirus death toll and new cases fall

Python Program to Find Armstrong Number in an Interval


To understand this example, you should have knowledge of following Python programming topics:
An Armstrong number, also known as narcissistic number, is a number that is equal to the sum of the cubes of its own digits. For example, 371 is an Armstrong number since 371 = 3*3*3 + 7*7*7 + 1*1*1.

Source Code


# Program to ask the user for a range and display all Armstrong numbers in that interval

# take input from the user
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))

for num in range(lower,upper + 1):
   # initialize sum
   sum = 0

   # find the sum of the cube of each digit
   temp = num
   while temp > 0:
       digit = temp % 10
       sum += digit ** 3
       temp //= 10

   if num == sum:
       print(num)
Output

Enter lower range: 100
Enter upper range: 1000
153
370
371
407
Here, we ask the user for the interval in which we want to search for Armstrong numbers. We scan through the interval and display all the numbers that meet the condition. We can see that there are 4 three digit Armstrong numbers. Visit here to find out how to check if a number is Armstrong number or not.

Comments