Print all Armstrong numbers between 1 to n

In this program, we will print all Armstrong numbers between 1 to n.

Steps to solve the program
  1. Take the last number up to which you want to print all Armstrong numbers.
  2. Create an empty list.
  3. Use for loop to iterate over all numbers from the 1 to given number.
  4. 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.
  5. Now divide the number by 10 using //.
  6. If the value of the variable is equal to the input number then it is an Armstrong number.
  7. Add that number to the empty list.
  8. 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
				
			

check whether a number is an Armstrong number or not

check whether a number is a Perfect number or not

Leave a Comment