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
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.
Example Input : 371 Output : It's an Armstrong Number Explanation : 371 can also be represented as 3^3 + 7^3 + 1^3 therefore it's an Armstrong Number.
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
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
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
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
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”)
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 “)
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”)
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’)
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”)
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’)
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.
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”)
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)
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”)
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”)
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”)
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