Check Armstrong Number in Python

What is Armstrong’s Number?

Armstrong number is a number in any given number base, which forms the total of the same number when each of its digits is raised to the power of the number of digits in the number. More simply, a number is an Armstrong Number if the sum of its digits raised to the power number of digits is equal to the original number. 

Do you still face difficulties in understanding the logic of the Armstrong Number? That’s perfectly fine, and we have something more for you. You can scroll down to learn more about checking Armstrong numbers using multiple methods.

Here you find all the Python practice Videos and Explanations.

Video Explanation to Check Armstrong Number in Python

Python Program to Check Armstrong Number

There are various methods to check Armstrong Numbers in python. The above video explains only one process to check Armstrong Number. But you can find source code and explanations of different methods over here. But before that, let us understand the basic logic of obtaining an Armstrong Number. Consider the number 153. To check if the number is Armstrong or not, we will do the addition of all the digits raised to the power of a total number of digits in the Number, so in our case, the equation will be like this.

1^3 + 5^3 + 3^3

Here 3 is the total number of digits in the number. If the result of the above equation is the same as the input, then our number is an Armstrong number. Let’s start with our program here.

Method 1: Check Armstrong Number in Python using the Math Module

from math import *
number = int(input("Enter the number : "))
result = 0
n = len(str(number))
temp = number
while (temp != 0):
    remainder = temp % 10
    result = result + pow(remainder, n)
    temp = int(temp/10)
if(result == number):
    print("The number is an Armstrong number")
else:
    print("The number is not an Armstrong number")
  

Code Explanation of Armstrong Number using Math Module

The above program is created to check whether the provided number is Armstrong Number or not. So, here we have imported math modules at the start of the program. Then at the beginning, we take a number from the user and then convert it back into an integer.

We declare a variable called result, which we will use to append in a while loop later to store the addition of all the digits raised to the power of a total number of digits. In the next line, we are checking the length of the number, which we have accepted from the user.

Remember, in the calculation of the Armstrong Number, we want the power of the base number to be nothing but the length of the number. We will declare a variable called temp and assign a value of the number to it, and we are using this for iteration of the while loop only.

Now start the while loop, which will run till the temp’s value becomes zero. Inside the loop first, we take the remainder of temp, which is nothing but the number. This will return digits from temp one by one as the loop executes. If the number is 153, the output will be like 3, then 5 and 1 simultaneously. So the loop will run 3 times.

Then we have done the addition of all the digits, raised to the power of a total number of digits in the number, and the same we are appending into the result variable. But wait, here we have used the pow function. What does it do? The pow function accepts two parameters: 1st is the base number, and 2nd is the power you want to calculate.

In the next line, we divide temp by 10 and convert it into an int, so it will only return the integer part. This is why when calculating the remainder, it will return values like 3, then 5, and 1.

In the next line, we will check whether the user input and our calculated results are the same or not. So what condition will we check if the result and name are the same? If yes, print “The number is an Armstrong number”; else, print “The number is not an Armstrong number”. I hope now the concept of Armstrong’s number is clear to you.

Output:
Enter the number :153
The number is an Armstrong number

Method 2: Check Armstrong number using the Recursion

import math as m

def getSum(num,num_length):
    if num == 0:
        return num
    else:
        return m.pow((num%10), num_length) + getSum(num//10,num_length)
		
num = int (input("Enter a number :"))
num_length = len(str(num))
sum = getSum(num,num_length)
if sum == int(num):
	print(num,"is an Armstrong Number.")
else:
	print(num,"is not an Armstrong Number.")
  

Code Explanation of Armstrong number using the recursion

Let’s try the Armstrong Number with another method from Recursion. Here also, we are importing the math library and using its alias as m. Then here, we have declared a function getSum which accepts two parameters. The 1st is num (which is provided by the user), and the second is num_lenght which is the length of the number.

Inside the function body, we will check if the number is zero. If it’s zero, then it will return the num, else it will calculate the sum. Inside the other section, we have calculated the addition of each digit raised to the total number of digits from the number.

Let’s divide this return statement from the else section into two parts. First is the pow function; as we know, pow() accepts two parameters. The first is the base, and the second is the exponent.

So as a first parameter, we provide the remainder of the num and the second parameter as num_length. If you check, we have written m.pow here. We have used the alias of the math library at the top of the program.

In the second part of the return statement, we use + to call the same function, nothing but Recursion. Remember, our function accepts two parameters, the number and length of the number.

So we write the function name, and for the first parameter, we will write num, then floor division operator, and 10, which will give the number’s remaining digits. The second parameter is the length of the number. This will continue till the num becomes 0.

Now, Outside of the function, we take a user number and then convert it into an integer. In the next line, we declare the variable num_length to store the length of the number.

Then we will call the getSum function with two parameters, num, and num_length, then the function will call recursively till the num value becomes zero, and the result is stored into a variable sum.

In the next line, we compare the result of the function stored in the sum and the number provided by the user that is stored in num.

If both variables have the same value, the program will print the number as an Armstrong number. Else Number is not an Armstrong number.

Output
Enter a number : 1634
1634 is an Armstrong Number.

About The Author