How to Find the Armstrong Number in Python using Recursion

Do you want to learn to check Armstrong numbers in Python using recursion? You have come to the right place.

In this Python article, we will discuss the Armstrong number in Python. Also, what is recursion, and why do we use it in Python? I will also explain, with an example, how to check the Armstrong number in Python using recursion.

What is Armstrong Number in Python?

An Armstrong number, also known as a narcissistic number. It is a special number equal to the sum of its digits. Each number is raised to the power of the number of digits.

For example, 371 is an Armstrong number because the sum of the cube(cube because it is a 3-digit number) of each digit, i.e., 3^3 + 7^3 + 1^3 = 371, is equal to the original number.

Check Armstrong Number in Python Using Recursion

Recursion in Python is a process in which a function calls itself directly or indirectly. It can be split down into smaller sub-problems utilizing recursion. It is the way of defining repetitively.

We can use Recursion to find the Armstrong number in Python, as it calculates the sum of the digits raised to the appropriate power.

Here is the complete Python program to check the Armstrong number using recursion in Python.

def count_digits(num):
    if num == 0:
        return 0
    return 1 + count_digits(num // 10)

def is_armstrong(num, digit_count):
    if num == 0:
        return 0
    return ((num % 10) ** digit_count) + is_armstrong(num // 10, digit_count)

def check_armstrong(num):
    digit_count = count_digits(num)
    return num == is_armstrong(num, digit_count)

number = int(input("Enter a number: "))
print(check_armstrong(number))

The below part of Python code is used to check if num is equal to 0; it means there are no more digits left, so it returns 0. If it is not, then extract the last digit of num using integer division (//) by 10 and add 1 to the result of count_digit for the remaining last part of num.

if num == 0:
return 0
return 1 + count_digits(num // 10)

Then, calculate the sum of each digit raised to the power of digit_count and add it to the result of is_armstrong.

return ((num % 10) ** digit_count) + is_armstrong(num // 10, digit_count)

Here, by calling the count_digita function, it calculates the count of digits in the number n. It returns True if the number num is equal to the result of the is_armstrong function having argument num and digit_count. Else, it returns False.

digit_count = count_digits(num)
    return num == is_armstrong(num, digit_count)

Output 1: here is the output of a number that is an Armstrong number.

Enter a number: 153
True

Refer to the screenshot below to see the result of the Python program to check the Armstrong number using recursion.

Check Armstrong Number in Python Using Recursion

Output 2: Here is an output of numbers that is not an Armstrong number:

Enter a number: 2458
False

After the code was implemented in the Pycharm editor, the subsequent screenshot was taken to illustrate the output.

How to find Armstrong number in Python

Conclusion

I hope the information provided in the above Python article gives a clear picture of the Armstrong number in Python and recursion in Python. Also, check the Armstrong number in Python using recursion with an example.

READ:  Merge Dictionaries in Python (8 different methods)

You may also like to read the following articles: