Program to check armstring number in python

Armstrong number:- 

                                    153=(1)^3 + (5)^3 +(3)^3
                                                    = 1+125+27
                                          = 153
                           So 153 is a armstrong number
                         2)  12=(1)^2 +(2)^2
                                          =1+4
                                  =5
                         So 12 is not armstrong number
 So we first of all calculate the number of digit and then seprate digits one by one from number by finding reminder and and calculate power of that digit with number of digit that is pow(digit, number_o_digit), and at last compaire  sum with number if equal then Armstrong otherwise not.

Program:-



n=int(input("Enter a number"))


no_of_digit=0


new_num=n '''number stored in another variable for use because after first


loop number become zero'''


while n!=0:


no_of_digit+=1


n=n//10


sum=0


n=new_num '''We store value of new_num in n because n is zero before


this step so we can not compaire'''


while new_num!=0:


r=new_num%10


mul=1


j=0


for j in range(no_of_digit):


mul=mul*r


sum=sum+mul


new_num=new_num//10


if sum==n:


print("Armstrong number")


else:


print("Not armstrong number")


Output:-
C:PycharmProjectspythonProject2venvScriptspython.exe C:/PycharmProjects/pythonProject2/nextprime.py
Enter a number153
Armstrong number

Process finished with exit code 0



Leave a Reply

Your email address will not be published.