Armstrong number is a number whose sum of the nth power of digits is equal to the number itself. Where n is called the order, which is equal to the number of digits present in the number.
For example:-
Let's take a number 371, which is an Armstrong number. Let's see
Order of number 371 (n) = 3
371 = 33 + 73 + 13
Let's take one more example of number 1634
Order of number 1634 (n) = 4
1634 = 14 + 64 + 34 + 44
Now you get the understanding of the Armstrong number, so, let's implement in a python program.
I will be discussing three ways to check for Armstrong number.
- By taking the number as int datatype.
- By taking the number as string datatype.
- And by using recursion.
Check Armstrong number by taking the number as int datatype
In this, we have taken the number as int datatype. And then we find the order of the number. And lastly passed the number to the loop to process it digit by digit.
Code:-
# taking input and storing in num variable
num = int(input("Enter a number: "))
# finding order using len() and str() function
order = len(str(num))
# initializing the sum variable by 0
sum = 0
# storing the num in temp variable
temp = num
# going through every digit of number store in temp variable
while temp > 0:
n = temp % 10
temp = temp // 10
# storing the sum of cube of the digits in sum variable
sum = sum + pow(n,order)
# checking whether the sum is equal to num or not
if(sum == num):
# printing the num is an Armstrong number
print(num,"is an Armstrong Number.")
else:
# printing the num is not an Armstrong number
print(num,"is not an Armstrong Number.")
Output:-
Enter a number: 371
371 is an Armstrong Number.
In this, we have used the temp variable and initialized to the number because we have to check the sum of the nth power of digits of the number with the given number, so we cannot manipulate the given number.
Check Armstrong number by taking the number as string datatype
The idea is same as above but the program is a little bit short and also the process.
Code:-
# taking input and storing in num variable
num = input("Enter a number: ")
# finding order using len() function
order = len(num)
# initializing the sum variable by 0
sum = 0
# going through every digit of number store in num variable
for i in num:
# storing the sum of cube of the digits in sum variable
sum += pow(int(i),order)
# checking whether the sum is equal to num or not
if sum == int(num):
# printing the num is an Armstrong number
print(num,"is an Armstrong Number.")
else:
# printing the num is not an Armstrong number
print(num,"is not an Armstrong Number.")
Output:-
Enter a number: 371
371 is an Armstrong Number.
Now let's discuss the last method.
Check Armstrong number by using recursion
In this, we have created a getSum() recursive function which will return the sum of the nth power of digits of the number. Rest is almost same.
Code:-
# defining the getSum() recursive function
def getSum(num):
if num == 0:
# Base case
return num
else:
# Iterative case
return pow((num%10),order) + getSum(num//10)
# taking input and storing in num variable
num = int(input("Enter a number: "))
# finding order using len() and str() function
order = len(str(num))
# initializing the sum variable by getting the sum from getSum()
sum = getSum(num)
# checking whether the sum is equal to num or not
if sum == int(num):
# printing the num is an Armstrong number
print(num,"is an Armstrong Number.")
else:
# printing the num is not an Armstrong number
print(num,"is not an Armstrong Number.")
Output:-
Enter a number: 1634
1634 is an Armstrong Number.
Hence, we have discussed all the ways.