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
- Take a number as input through the user.
- See the given example to understand what is an Armstrong number.
- Create a variable and assign its value equal to 0.
- 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.
- 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