In this program, we will print all Armstrong numbers between 1 to n.
Steps to solve the program
- Take the last number up to which you want to print all Armstrong numbers.
- Create an empty list.
- Use for loop to iterate over all numbers from the 1 to given number.
- While the number is greater than 0 divide the number by 10 using % and add the remainder raised to 3 to the variable that we have created.
- Now divide the number by 10 using //.
- If the value of the variable is equal to the input number then it is an Armstrong number.
- Add that number to the empty list.
- Print the output.
limit = int(input("Enter the last number: "))
# Iterate over all the numbers
for i in range(1, limit + 1):
total = 0
temp = num = i
while temp > 0:
digit = temp % 10
total += digit ** len(str(i))
temp //= 10
if num == total:
print(num, end=" ")
Output :
Enter the last number: 500
1 2 3 4 5 6 7 8 9 153 370 371 407