In this article, we shall have a look at python program to check for an Armstrong number with explanation and output. The program will read the number as user input and check if it is Armstrong number or not.

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 and 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.

Program for Armstrong number in Python
Python program to check for Armstrong number based on above algorithm is given below.

```# 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 using python’s built-in `input` function. `input()` returns value in string format which is converted to integer format using another built-in int function.

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 using python’s if-else statement.

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 article helped in checking a number for an Armstrong number. Click the clap below!!!

0

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.