How to check for Armstrong number in python

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 = a3 + b3 + c3 + d3

then abcd is Armstrong.

How to check for Armstrong number
In order to check if a number is an Armstrong number, following algorithm can be used.

  1. Initialize a variable to hold the sum of cube of digits.
  2. Loop over the digits of number.
  3. Calculate cube of each digit.
  4. Add this cube to the variable initialized in step 1.
  5. 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 built-in input function. It returns value in string format which is converted to integer format using another built-in 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

    1. 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.
    2. 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.
    3. 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.

It is also possible to calculate cube of a number using python’s 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

Mark Your Impression

  Subscribe  
Notify of
Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.