Python program to check a number is Armstrong number or not.

  

 What is a Armstrong number:


Python program to check Armstrong  number:

def armstrong(n):

number = str(n)


n = len(number)

output = 0

for i in number:

output = output+int(i)**n


if output == int(number):

return(True)

else:

return(False)

print(armstrong(153))  #True

print(armstrong(120)) #False

OR

i=0

result=0

n = int(input("please give a number : "))

number1 = n

temp = n

while n!=0:

    n = (n//10)

    i=i+1;

while number1!=0:

    n=number1%10

    result=result+pow(n,i)

    number1=number1//10

if temp==result:

    print("number is armstrong")

else:

    print("number is not armstrong")


 

Post a Comment

0 Comments