Check if a number is an Armstrong number or not

In this program, we will check whether a number is an Armstrong number or not.
Definition : An Armstrong number is a one whose sum of digits raised to be the power of each digits by count of total digit.

Armstrong Number Example 371 :  3**3 + 7**3 + 1**3 = 371

Steps to solve the program
  1. Take a number as input through the user.
  2. See the given example to understand what is an Armstrong number.
  3. Create a variable and assign its value equal to 0.
  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. Print the output.
				
					num = int(input("Enter a number: "))
total = 0
temp = num

while temp > 0:
    # get last digit one by one
    digit = temp % 10
    total += digit**len(str(num))
    temp //= 10

if num == total:
    print(num,"is an Armstrong number")
else:
    print(num,"is not an Armstrong number")
				
			

Output :

				
					Enter a number: 153
153 is an Armstrong number
				
			

find all prime factors of a number

print all Armstrong numbers between 1 to n

Leave a Comment