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”


  • krish

    num=input()
    sum=0
    for i in num:
    sum=int(i)**len(num)+sum
    if sum==int(num):
    print(“yes”)
    else:
    print(“NO”)


  • Abhishek

    import math
    n=(input(“enter number”))
    sum=0
    for i in n:
    sum=sum+math.pow(int(i),3)
    if sum==int(n):
    print(“armstrong”)


  • Kaustab

    n = int(input(“Enter the number: “))
    i = len(str(n))
    t = n
    s = 0
    while t!=0:
    r = t%10
    s = s + (r**i)
    t = t//10
    if n == s:
    print(f'{n} is an Armstrong Number’)
    else:
    print(f'{n} is not an Armstrong Number’)


  • Kaustab

    Simple way without using array-

    n = int(input(“Enter the number: “))
    i = len(str(n))
    t = n
    s = 0
    while t!=0:
    r = t%10
    s = s + (r**i)
    t = t//10
    if n == s:
    print(f'{n} is an Armstrong Number’)
    else:
    print(f'{n} is not an Armstrong Number’)


  • M

    num = int(input(“enter the number:”))
    sum= 0
    temp = num
    while num > 0:
    remainder = num % 10
    sum += remainder ** 3
    num = num // 10
    if sum == temp:
    print(sum ,”is a armstrong number”)
    else:
    print(sum ,”is not a armstrong number”)


  • Juhi

    num = int(input(“Enter a number: “))
    rem = 0
    rev = 0
    org = num
    while( num >0):
    rem = num %10
    rev = rev + rem ** 3
    num = num // 10
    if (org == rev):
    print(“Armstrong number”)
    else:
    print(“Not Armstrong”)


  • Anubhav

    n = input()
    sum = 0
    for i in n:
    sum = int(sum) + int(i)*int(i)*int(i)
    if sum == int(n):
    print(f”{n} is an armstrong number”)
    else:
    print(“it is not an armstrong number”)


  • Priya

    n=int(input())
    temp=n
    sum=0
    while n>0:
    r=n%10
    sum=sum+r**3
    n=n//10
    if sum==temp:
    print(“armstrong no”)
    else:
    print(“not armstrong no”)


  • URK18CS211

    num=int(input(“enter the number”))
    num1=num
    sum=0
    while(num1!=0):
    temp=num1%10
    sum=sum+temp*temp*temp
    num1=num1//10
    if(num==sum):
    print(“armstrong”, num)
    else:
    print(“not an armstrong”, num)


  • KIRA

    x = int(input(“Enter a Number : “))

    y = str(x)
    z = len(y)
    count = 0
    count1 = 0

    for i in y:
    i = int(i)

    count1 = i ** z
    count += count1

    if count == x:
    print(“Number is Armstrong”)
    else:
    print(“Not Armstrong”)

    print(count)


  • ROHAN

    n = input()
    total = 0
    for i in n:
    total += int(i)**3
    if total == n:
    print(“True”)
    else:
    print(“false”)


  • 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”)