on
Italy
- Get link
- X
- Other Apps
# 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)
OutputEnter lower range: 100 Enter upper range: 1000 153 370 371 407Here, 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
Post a Comment