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
a=int(input(“enter”))
s=a
c=len(str(n))
sum=0
while n!=0:
r=n%10
sum=sum+(r**c)
n=n//10
if s==sum:
print(“Armstrong”)
else:
print(“not a armstorng number”)
# 13.Check Whether a Given Number is an Armstrong Number or Not in Python.
m=int(input(“Enter the num:”))
n=str(m)
a=len(n)
lst=[]
for i in n:
z=int(i)
fact=0
fact=z**a
lst.append(fact)
b=int(n)
sum=0
for j in lst:
sum=sum+j
if (sum==b):
print(f”{n} is a Armstrong.”)
else:
print(f”{n} is not a Armstrong.”)
n = 1634
temp = n
find_len = len(str(n))
# print(find_len)
sumi =0
while n!=0:
last_dig=n%10
sumi += pow(last_dig,find_len)
n=n//10
if(sumi == temp):
print(“it is a armstrong number “)
else:
print(“not an armstrong number”)
number = 371
num = number
sum = 0
length = len(str(num))
for i in range(length):
digit = num%10
num = num//10
sum =sum+(digit**length)
if sum==number:
print(“Armstrong”)
else:
print(“Not Armstrong”)
num = int(input(“enter the num: “))
x = len(str(num))
sum = 0
for i in str(num):
num1 = int(i)**int(x)
sum = sum + num1
if sum == 370:
print(num,”is a armstrong number”)
else:
print(num,”is not a armstrong number”)
num=input(“Enter a number : “)
temp=num
c=0
for i in num:
c+=int(i)**len(num)
temp=int(i)//10
if num==c:
print(“Its a armstrong number”)
else:
print(“its not a armstrong number”)
a=int(input(“Enter any number to check its armstrong or not: “))
b=str(a)
d=len(b)
sum=0
for i in b:
c=int(i)**d
sum+=c
if sum==a:
print(“its an armstrong number”)
else:
print(“its not an armstrong number”)
a = input()
n = int(a)
b = list(a)
c = len(a)
s = 0
for i in range(c):
f = int(b[i])
s += pow(f, c)
# print(s)
if s == n:
print(“ARM..”)
else:
print(“not ARM…”)