Armstrong Number in Python

Check Whether a Given Number is an Armstrong Number or Not in Python

For a given integer the objective is to check whether or not the given integer is an Armstrong number or not. The Armstrong number is briefly defined in the section below.

Example
Input : 371
Output : It's an Armstrong Number
Python Program to find a Number is Armstrong or not

Check Whether a Given Number is an Armstrong Number or Not

Given an integer input, the objective is to check whether or not the given input variable is an Armstrong number. In order to do so, we check whether the sum of the digits of each number to the power the length of the number is equal to the number itself or not. If the number is the same as the original, it’s an Armstrong number. Mentioned below are a few of the Methods used to solve this problem,

  • Method 1: Using Iteration
  • Method 2: Using Recursion

We’ll see the working of the above mentioned methods in the upcoming sections. Please go through the below mentioned blue box for better understanding of the problem.

Armstrong NumberIn Python

Method 1: Using Iteration

In this method we’ll use for loop and while loop to check for Armstrong number.

Working

For an integer input number, we perform the following operations

  • We break down the number into digits using “%” operator.
  • We raise the digit to the power of the length of the number.
  • Keep appending the value to sum variable.
  • Check if the sum variable matches the number itself.
  • If the above condition is satisfied, it’s an Armstrong Number.

Let’s implement the above logic in Python Language.

Python Code

Run
number = 371
num = number
digit, sum = 0, 0
length = len(str(num))
for i in range(length):
  digit = int(num%10)
  num = num/10
  sum += pow(digit,length)
if sum==number:
  print("Armstrong")
else:
  print("Not Armstrong")

Output

Armstrong

Method 2: Using Recursion

In this method we’ll use recursion to check for Armstrong number. To know more about recursion, check out Recursion in Python.

Working

For a given integer input, we do the following

  • Define a recursive function checkArmstrong() with base case as num==0.
  • Set the recursive step call as checkArmstrong(num/10, length, sum).

Let’s implement the above logic in Python Language.

Python Code

Run
number = 371
num = number
sum =0
length = len(str(num))
def checkArmstrong(num,length,sum):
  if num==0:
    return sum
  sum+=pow(int(num%10),length)
  return checkArmstrong(num/10,length,sum)

if checkArmstrong(num,length,sum)==number:
  print('Armstrong')
else:
  print("Not Armstrong")

Output

Armstrong

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Related Pages

45 comments on “Armstrong Number in Python”


  • pamidi

    n=int(input(“enter a number : “))
    a=str(n)
    s=0
    for i in a:
    s=s+int(i)**3
    if(s==n):
    print(“Armstrong number”)
    else:
    print(“not a armstrong number”)


  • Nilavo

    a=input(“enter the number: “)
    sum=0
    for i in a:
    c=int(i)**3
    sum=sum+c
    if str(sum)==a:
    print(a,”is a armstrong number”)
    else:
    print(a,”is not armstrong number “)


  • BHANU

    a=input()
    ans=0
    b=len(a)
    for i in a:
    ans=ans+int(i)**b
    print(ans)
    if ans==int(a):
    print(“A”)
    else:
    print(“NA”)


  • Ayaan

    I feel this is much more simple:
    num=int(input())
    list=[int(i) for i in str(num)]
    sum=0
    for i in list:
    sum=(sum+i**len(list))
    if sum==num:
    print(‘it is an armstrong number’)
    break
    else:
    print(‘it is not’)


  • gunjan

    num=int(input(“enter number”))
    temp=num
    order=len(str(num))
    sum=0
    while(num>0):
    digit=num%10
    sum=sum+digit**order
    num=num//10
    if(temp==sum):
    print(“armstrong”)
    else:
    print(“not”)


  • Bharath

    n = int(input(‘Enter a number: ‘))
    temp = [int(d) for d in str(n)]
    n2 = []

    for i in temp:
    a = i ** 3
    n2.append(a)
    if n == sum(n2):
    print(‘Amstrong number’)
    else:
    print(‘Not an Amstrong number’)


    • BHANU

      For a three digit number only, you use sum of cubes of digits of the number to check whether it is an Amstrong number or not. The power of a digit depend upon the number of digits in the given number, but not three always.


  • pl

    n= input(“Enter the Number: “)
    temp=int(n)
    li=list(n)
    num=list(map(int,li))
    sum = 0
    for i in range(len(num)):
    sum = sum + pow(num[i], len(num))
    if sum==temp:
    print(“Given number is Armstrong Number”)
    else:
    print(“Given Number is not Armstrong Number”)


  • Bibhudutta

    num=int(input(‘Enter the number:’))
    temp=num
    order=len(str(num))
    sum=0

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

    if(sum==temp):
    print(‘The number is Armstrong’)
    else:
    print(‘The number is not Armstrong)


  • mohammadnawaz111

    n=int(input(“Enter the Number : “))
    order=len(str(n))
    sum=0
    temp=n
    while temp>0:
    digit=temp%10
    sum+=digit**order
    temp//=10
    if n==sum:
    print(“Armstrong Number”)
    else:
    print(“Not an Armstrong Number”)


  • p

    I THINK THIS IS BETTER AND EASY TO UNDERSTAND 🙂
    n=input(“n:”)
    power=len(str(n))
    sum=0
    for k in n:
    sum+=int(k)**power
    if str(sum)==str(n):
    print(“armstrong”)
    else:
    print(“not armstrong”)


  • Anirudh

    n = int(input())
    m = str(n)
    a = len(m)
    print(“The Num is “,n)

    sum = 0
    for i in m:
    sum = int(i)**a + sum

    if sum == n:
    print(“It is Amstrong num”)
    else:
    print(“It is Not Amstrong num”)


  • 44_Harsh

    n = int(input(“enter no. to check : “))
    sum = 0
    pow = len(str(n))
    n1 = n
    while(n>0):
    digit = n%10
    sum += digit**pow
    n=n//10

    if sum == n1:
    print(f”{n1} is an Armstrong number”)
    else:
    print(f”{n1} is not an Armstrong number”)
    #OUTPUT:
    enter no. to check : 167
    167 is not an Armstrong number