fastread homefastrread library fastread menu

Python : Check Armstrong Number

Tutorial by:Rizwan Abdullah      Date: 2016-06-16 00:59:24

❰ Previous Next ❱

An Armstrong number, also known as narcissistic number, is a number that is equal to the sum of the cubes of its own digits. For example, 370 is an Armstrong number since 370 = 3*3*3 + 7*7*7 + 0*0*0.

Source Code

# Python program to check if the number provided by the user is an Armstrong number or not

# take input from the user
num = int(input("Enter a number: "))

# initialise sum
sum = 0

# find the sum of the cube of each digit
temp = num
while temp > 0:
   digit = temp % 10
   sum += digit ** 3
   temp //= 10

# display the result
if num == sum:
   print(num,"is an Armstrong number")
   print(num,"is not an Armstrong number")

Output 1

Enter a number: 663
663 is not an Armstrong number



Output 2

Enter a number: 407
407 is an Armstrong number

Here, we ask the user for a number and check if it is an Armstrong number. We need to calculate the sum of cube of each digit. So, we initialize the sum to 0 and obtain each digit number by using the modulus operator %. Remainder of a number when it is divide by 10 is the last digit of that number. We take the cubes using exponent operator. Finally, we compare the sum with the original number and conclude that it is Armstrong number if they are equal.

❰ Previous Next ❱


Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website