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”


  • Sriram

    user_input = input(“Enter a number: “)
    sum = 0

    for _ in user_input:
    sum += int((_))**len(user_input)

    if sum == int(user_input):
    print(“It’s a armstrong number”)
    else:
    print(“It’s not a armstrong number”)


  • Syed

    num = int(input(“Enter a number: “))

    # initialize sum
    sum = 0

    # find the sum of the cube of each digit
    temp = num
    while temp > 0:
    digit = temp % 10
    sum += digit ** 3
    temp //= 10

    # display the result
    if num == sum:
    print(num,”is an Armstrong number”)
    else:
    print(num,”is not an Armstrong number”)


  • Pavithra

    n=input()
    d=0
    k=[i for i in n]
    l=len(k)
    for j in k:
    p=int(j)
    d=d+(p**l)
    f=str(d)
    h= [n for n in f]
    if (h == k):
    print(“Armstrong”)
    else:
    print(“not Armstrong”)


  • premkumar

    Find a Number is Armstrong or not:
    n=int(input())
    t=n
    s=0
    while n!=0:
    r=n%10
    s=s+(r**3)
    n=n//10
    print(s)
    if t==s:
    print(f”{s} is a armstrong number”)
    else:
    print(f”{s} not a armstrong number”)


  • Afaque

    x=input(‘Enter the number to be checked :’)
    l=len(x)
    s=0
    for i in x:
    s+= pow(int(i),l)

    if (s==int(x)):
    print(‘Armstrong !!!’)
    else:
    print(‘Not an Armstrong !!!’)


  • Ega

    import math
    k=int(input(‘num’))
    l=[int(d) for d in str(k)]
    s=0
    while(k>0):
    r=k%10
    s=s+math.pow(r,len(l))
    k=k//10
    print(‘pal’,s)


  • Giri

    q=int(input(“enter the num:”))
    c=str(q)
    s=[]
    for i in c:
    d=int(i)**3
    s.append(d)
    a=sum(s)
    if c in str(a):
    print(“yes it is a armstrong num”)
    else:
    print(“no it is not a armstrong number”)


  • fiza

    Without importing libraries
    num = int(input(‘enter: ‘))
    num1 = str(num)
    print(num1)
    total_num = len(num1)
    total = 0
    for i in num1:
    total = total + int(i)**total_num
    print(total)

    if num1 == str(total):
    print(‘Armstrong number’)
    else:
    print(‘not armstrong number’)


  • Sourabh

    another approach:
    n = int(input(‘enter the no’))
    result = 0
    original = n
    order = len(str(n))
    while n > 0:
    digits = n % 10
    result += digits ** order
    n = n // 10

    if result == original:
    print(‘no is armstrong’)
    else:
    print(‘no is not armstrong’)


  • sweta

    num=int(input(“enter no:”))
    temp=num
    s=0
    while num>0:
    r=num%10
    num=num//10
    s=s+(r)**3
    print(s)
    if(s==temp):
    print(“amstrong”)
    else:
    print(“not amstrong”)


  • Gunjan

    x=int(input(‘enter the no :’))
    r=0
    u=len(str(x))
    m=x
    while(x!=0):
    rem=x%10
    r=r+pow(rem,u)
    x=x//10
    print(r)
    if(r==m):
    print(‘amstrong no’)
    else:
    print(‘not amstrong’)
    def pow(g,a):
    while(a!=0):
    a=a-1
    i=g*pow(g,a)
    return i
    By-gunjan soni


  • anubhav

    n=int(input())
    k=len(str(n))

    temp=n
    sum=0
    while(n!=0):
    d=n%10
    sum=sum+d**k
    n=n//10
    if(temp==sum):
    print(“yes”)
    else:
    print(“no”)