Armstrong number in Python

Written by

Himani Kohli

Armstrong Number Program in Python

In this program where we are going to learn whether an N-digit integer is an Armstrong number or not.

A number is said to be an Armstrong number if it is equal to the sum of the cubes of its own digit.

Example: 153 is an Armstrong number, 153=1*1*1 + 5*5*5 + 3*3*3

Algorithm: 

  1. Input from the user.
  2. Take the length of the number.
  3. Assign a = n
  4. Initialize arm = 0.
  5. Using for loop, find whether a given number is Armstrong or not.
  6. Using if condition, if arm !=n then print it is not an Armstrong.
  7. Else print it is an Armstrong number.
  8. Exit 

Code:

n=input("Enter a number :")

l=len(n)

n=int(n)

a=n

arm=0

for i in range(l+1):

    b=a%10

    a=a/10

    arm=arm+(b**l)

if (arm != n):

    print("It is not an Armstrong no.")

else:

    print("It is an Armstrong no.")

 

Output: 

Enter a number :153

It is an Armstrong no.

Armstrong number in Python