python

Python Program to Check Armstrong Number

In this tutorial, we are going to see how to write a Python program to check Armstrong number. A positive number is called an Armstrong number if it is equal to the sum of the cubes of its digits, e.g. 0, 1, 153, 370, 371, 407, etc.

That is to say, the following equation will be verified.

xy..z = xn + yn + ….. + zn

n is the number of digits.

For example, 370 is a 3-digit Armstrong number.

370 = 33 + 73 + 03
        = 27 + 343 + 0
        = 370

Now we will see the implementation of the Armstrong number in Python.
 

 

Python Program to Check Armstrong Number
# Ask the user to enter a number
nbr = int(input("Enter a number: "))

# initialize the sum variable
s = 0

# find the sum of the cube of each digit
tmp = nbr
while tmp > 0:
   d = tmp % 10
   s += d ** 3
   tmp //= 10

# Print the result
if nbr == s:
   print(nbr," is an Armstrong number")
else:
   print(nbr," is not an Armstrong number")

Output:

Enter a number: 370
370 is an Armstrong number
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *