What is Armstrong number
A number whose sum of cube(number raised to power of 3) of its digits is equal to the number itself is an Armstrong number. Thus, if abcd is a number, then if
abcd = a^{3} + b^{3} + c^{3} + d^{3}
then abcd is Armstrong.
In order to check if a number is an Armstrong number, following algorithm can be used.
 Initialize a variable to hold the sum of cube of digits.
 Loop over the digits of number.
 Calculate cube of each digit.
 Add this cube to the variable initialized in step 1.
 After all digits of number have been covered, compare the value of sum with the number to determine if they are same or not.
Check for Armstrong number in Python
Python program to check for Armstrong number is given below. It is based on the above algorithm.
# read number from user number = int(input("Enter a number")) # store number for later use copy = number # initialize variable to hold sum sum_of_digits = 0 # iterate over number while number > 0: # get the last digit of number digit = number % 10 # calculate cube of digit and add it to sum sum_of_digits += digit ** 3 # remove last digit from number number= number // 10 # compare sum of digits with actual number if sum_of_digits == copy: print("Number is Armstrong") else: print("Number is not Armstrong") 
Explanation
Program reads a number from user. This is done by using python’s builtin input
function. It returns value in string format which is converted to integer format using another builtin function int
. A copy of input number is made so that it can be compared with the sum later and a variable is initialized to store the sum of cube of digits.
Now we iterate over the digits of number. In each iteration, following steps are performed

 Last(or rightmost) digit of number is extracted by using modulus(%) operator with 10. This will return the remainder of division of number by 10.
 Cube of this last digit is calculated by using exponent(**) operator with 3 and is added to the the variable initialized to hold the sum.
 Last digit of number(whose cube was calculated in previous step) is removed by using integer division(//) with 10 and the result is assigned back to the original number.
Integer division will return the quotient of the number after division with 10, that is, 5432 // 10 = 543. It will give 0 when all digits of the number have been removed, that is why, we are iterating till number > 0.
After the loop completes, we compare the value of sum with the original number and print the result accordingly.
power
function which accepts two arguments: first is the number which will be raised to the power and second is the power to which it will be raised. Thus, to calculate cube of 5, power(5, 3)
can be used.Output
Different executions of above program with various user input result in following output
Enter a number
217
Number is not Armstrong
Enter a number
371
Number is Armstrong
Enter a number
371
Number is Armstrong
Enter a number
5234
Number is not Armstrong
Hope this post helped in checking a number for an Armstrong number. Keep visiting for more!!!
0
Leave a Reply