Check Armstrong Number in Python

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 come up with the best video explanation you have ever watched on the internet, where you not only understand the logic to find Armstrong Number but also code to find it.

You still face difficulties in understanding the logic for Armstrong Number? That’s perfectly fine, 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.

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 method to check Armstrong Number. But you can find source code and explanation of different methods over here

But before that let us understand basic logic obtaining an Armstrong Number.

Consider a number 153.

To check 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 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 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 are declaring a variable called result which we will use to append in while loop later to store 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 Armstrong Number we want the power of base number that power will nothing but the length of the number.

We will declare a variable called temp and assigning a value of the number to it, we are using this for iteration of while loop only.

Now start the while loop, which will run till the temp’s value becomes zero. Inside while loop first we are taking 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 then 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 same we are appending into result variable.

But wait, here we have used the pow function. But what it does? pow function accepts two parameters 1st is the base number and 2nd is the power that you want to calculate of that number.

In the next line, we are dividing temp by 10 and converting it into an int, so it will only return the integer part. This is the reason whenever 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 result are the same or not. So herewith if condition we will check result and name are 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 Armstrong Number with another method from Recursion.

Here also we are importing math library and using its alias as m.

Then here we have declared a function getSum which accepts two parameters 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 will calculate the sum.

Inside else section we have done our calculation of addition of each digit raised to total number of digit from number.

Let’s divide this return statement from else section into two parts.

First is of pow function, as we know pow() accepts two parameters, first the base and second is the exponent.

So as a first parameter we are providing the remainder of the num and the second parameter as num_length. If you check we have written m.pow here this is just we have used the alias of math library at the top of the program.

In the second part of the return statement we use + then again call the same function which is nothing but recursion.

Remember our function accepts two parameters, the number, and length of the number.

So we write function name and for the first parameter, we will write num then floor division operator, and 10 which will give the remaining digits of the number.

The second parameter is the length of the number. This will continue till the num became 0.

Now, Out side of the function we take a number from the user and then converted it into an integer.

In the next line we declare variable num_length to store 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 became zero, and the result is stored into variable sum.

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

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

So, Now you are all good to go for an interview questions or any tricky questions on Armstrong’s Number.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *