Python Program to Check Armstrong Number Using Functions

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

Let’s have a glance at how to check Armstrong’s number using the function in Python programming language.

What is Armstrong’s number?

Armstrong number is a number that is equivalent to the sum of cubes of its digits. For example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers. Let’s try to understand why 407 is an Armstrong number.
(4*4*4)+(0*0*0)+(7*7*7)=407

Python Program to Check Armstrong Number Using Functions

def check_power(a, x): 
if x==0: 
return 1
if x%2==0: 
return check_power(a, x/2)*check_power(a, x/2) 
return a*check_power(a, x/2)*check_power(a, x/2)

def check_order(x):

n = 0
while (x!=0): 
n = n+1
x = x/10
return n

# Function to check whether the given number is Armstrong number or not 
def isArmstrong (x): 
n = 0
while (x!=0): 
n = n+1
x = x/10
n = check_order(x) 
temp = x 
sum1 = 0
while (temp!=0): 
r = temp%10
sum1 = sum1 + check_power(r, n) 
temp = temp/10

return (sum1 == x)

x = 407
if isArmstrong(x)==1:
print x, 'is an armstrong number'

Output:

407 is an armstrong number

Leave a Comment

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