Finding Armstrong numbers can be a tricky task. Hence many interviews or exams feature this problem.

So, what is Armstrong 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.

In a more simple way, 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.

We want you to know about Armstrong Number. Hence, we are coming up with the best video explanation you have ever watched on the internet, where you will understand the logic to find Armstrong Number with the help of python code.

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

*For all the practice Videos and Explanation on Python please click over here. Python Practice Series.*

Table of Contents

## Video Explanation to Check Armstrong in Python

## Code: To Print Armstrong Number in Python

There are various methods to check Armstrong Number 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 a 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.

**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.

Lets start with our program here.

### Method 1: Check Armstrong Number using Math Module

#### Source Code and Output

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")

Output:

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

**Code Explanation Method 1: 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? 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 remainder, it will return values like 3, then 5, and 1.

In the next line, we will check whether 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.

### Method 2: Check Armstrong number using the Recursion

#### Source Code and Output

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.")

Output

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

**Code Explanation Method 2: 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 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.

So, now you are all good to go for interview questions or any tricky questions on Armstrong’s number.