Armstrong number
An Armstrong number is a number that is equal to the sum of the digits that rise to the total number of digits of the power. Some Armstrong numbers are: 0, 1, 2, 3, 153, 370, 407, 1634, 8208, etc.
Know more about Armstrong number – Wikipedia
Logic
After taking input from the user, we have to calculate the total number of digits in the given number. Here we are using log10 function of the math module to achieve this. After doing that we have to run one loop until the value of user given number “num” does not reach 0 or becomes less than zero.
Now inside the loop each time we have to find the last digit of user given number and then calculate the power of that number with total no of digits in the “num”, round off the result and store it in one temporary variable “sum” once the loop is finished we have to check if the value of variable sum is equivalent to user given number, if yes, then it is an Armstrong number.
Program
import math # Take input from user num = int(input("Enter number to check : ")); originalNum = num; # Find total digits in given number digits = int(math.log10(num) + 1); sum = 0 while(num > 0): lastDigit = int(num % 10); sum = sum + round(math.pow(lastDigit, digits)); # Remove the last digit num = num / 10; # Empty print statement for new line print() if originalNum == sum: print(originalNum, "is ARMSTRONG NUMBER") else: print(originalNum, "is NOT ARMSTRONG NUMBER")
Output
Enter number to check : 1634
1634 is ARMSTRONG NUMBER