Armstrong number program in python with explanation

Ans: 

Welcome back, In this tutorial we are going to learn how to write a program in python to check a given input number is Armstrong or not?

So before moving directly on the writing program I will recommend you please first know :

What is Armstrong Number?

  • It is a number which is equal to the sum of cube of its digits.
  • For eg: 153, 370 etc.

Lets see it practically by calculating it,

Suppose I am taking 153 for an example 

First calculate the cube of its each digits

  1^3 = (1*1*1) = 1

  5^3 = (5*5*5) = 125

  3^3= (3*3*3) = 27

Now add the cube 

  1+125+27 = 153

It means 153 is an Armstrong number.

 

Ok, Then move on the program when you have cleared concept 🙂

Program to check a number is Armstrong or not in python programming language

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") 

Output:

armstrong program in python