Latest posts

10/recent/ticker-posts

Armstrong Number program in Python | Check whether an Number is Armstrong or Not !

Armstrong Number program in Python




I have created a Program to check an Number is Armstrong Number or Not !

So we need to know that what is Armstrong Number.

Suppose, 153 is a Number, Which has 3 digit in it. So in armstrong logic, we seperates each digit from this number like, 1, 5, 3.
So now we know that in this given number, we have 3 digits, that's why we have to take the cude of each digit and then add all the results.
Ex,

153 = 1 x 1 x 1 + 5 x 5 x 5 + 3 x 3 x 3 
       =  1 + 125 + 27
       = 153


So, this is nothing but the logic of the armstrong number.

Now take a overview of our program,

Firstly we need to take a number from user. for we used,

    num = int(input("Enter a Number : "))

we created a variable with name 'num' and in that we stored those number which is entered by user.

To take the input from user, we used input() function.

After that, we created two more variable 'temp' to store our actual number and 'sum' for addition.

    temp = num
    sum = 0
    order = len(str(num))

we created one another variable 'order' to get the length of our number,
for that we used a funtion len(). This function is basically used to get the lenght characters in a string. That's why we have typecasted out 'num' as a string by useing str(num)

So at the end our variable order will returns the count of digit, present in number given by user.

After that, we used a while-loop to iterate a statement until the given condition in not breaks.

    while(num > 0):
        digit = num % 10
        sum = sum + digit ** order
        num = num // 10

so in this while loop, we passed a condition, num > 0 , because of this our while loop will iterates until our 'num' becomes zero.

After that, we have entered our ogic of armstrong number in while loop.
You can whatch the Armstrong Numbers logic in our youtubes video (Link is given in bottom of the post ).

After thay, we used if-else statment to check our number is an Armstrong Number or Not !


    if(sum == temp):
        print(f"{temp} is a Armstrong Number !")
        print("")
        print("")
    else:
        print(f"{temp} is not a Armstrong Number !")
        print("")
        print("")



Output : 


    Find Armstrong Number !

    Enter a Number : 153  

    153 is a Armstrong Number !



Try yourself : 













Also Visit


Post a Comment

0 Comments