 # Python Program to Check Armstrong Number using Recursion

In the previous article, we have discussed Python Program to Print Multiplication Table using Recursion

Given a number and the task is to check whether the given number is an Armstrong number or not using recursion.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given Number = 153

Output:

The above given Number is an Armstrong Number.

Example2:

Input:

Given Number = 100

Output:

The above given Number is Not an Armstrong Number.

## Program to Check Armstrong Number using Recursion in Python

Below are the ways to check whether the given number is an Armstrong number or not using recursion:

### Method #1: Using Recursion (Static Input)

Approach:

• Give the number as static input and store it in a variable.
• Take a variable say rslt_sum and initialize its value to 0.
• Pass the given number as an argument to the chek_ArmstrngNumb function.
• Create a recursive function to say chek_ArmstrngNumb which takes the given number as an argument and returns the rslt_sum.
• Make the rslt_sum a global declaration.
• Check if the given number is not equal to 0 using the if conditional statement.
• If the statement is true, get the last digit of the given number using the modulus operator and store it in another variable k.
• Calculate the value of k raised to power 3 using the pow() function and add it to the rslt_sum.
• Store it in the same variable rslt_sum.
• Pass the given number divided by 10 as an argument to the chek_ArmstrngNumb function(which removes the last digit of the number) {Recursive Logic}.
• Return rslt_sum.
• Check if the chek_ArmstrngNumb which takes the given number as an argument is equal to the given number using the if conditional statement.
• If the statement is true, then print “The above-given number is an Armstrong Number”.
• Else print “The above-given number is not an Armstrong Number”.
• The Exit of the Program.

Below is the implementation:

# Create a recursive function to say chek_ArmstrngNumb which takes the given number as
# an argument and returns the rslt_sum.

def chek_ArmstrngNumb(gven_numb):
# Make the rslt_sum a global declaration.
global rslt_sum
# Check if the given number is not equal to 0 using the if conditional statement.
if (gven_numb != 0):
# If the statement is true, get the last digit of the given number using the modulus
# operator and store it in another variable k.
k = gven_numb % 10
# Calculate the value of k raised to power 3 using the pow() function and add it to the
# rslt_sum.
# Store it in the same variable rslt_sum.
rslt_sum += pow(k, 3)
# Pass the given number divided by 10 as an argument to the chek_ArmstrngNumb function
# (which removes the last digit of the number) {Recursive Logic}.

chek_ArmstrngNumb(gven_numb//10)
# Return rslt_sum.
return rslt_sum

# Give the number as static input and store it in a variable.
gven_numb = 153
# Take a variable say rslt_sum and initialize its value to 0.
# Pass the given number as an argument to the chek_ArmstrngNumb function.
rslt_sum = 0
# Check if the chek_ArmstrngNumb which takes the given number as an argument is equal
# to the given number using the if conditional statement.
if (chek_ArmstrngNumb(gven_numb) == gven_numb):
# If the statement is true, then print "The above given number is an Armstrong Number".
print("The above given Number is an Armstrong Number.")
else:
# Else print "The above given number is not an Armstrong Number".
print("The above given Number is Not an Armstrong Number.")


Output:

The above given Number is an Armstrong Number.

### Method #2: Using Recursion (User Input)

Approach:

• Give the number as user input using the int(input()) function and store it in a variable.
• Take a variable say rslt_sum and initialize its value to 0.
• Pass the given number as an argument to the chek_ArmstrngNumb function.
• Create a recursive function to say chek_ArmstrngNumb which takes the given number as an argument and returns the rslt_sum.
• Make the rslt_sum a global declaration.
• Check if the given number is not equal to 0 using the if conditional statement.
• If the statement is true, get the last digit of the given number using the modulus operator and store it in another variable k.
• Calculate the value of k raised to power 3 using the pow() function and add it to the rslt_sum.
• Store it in the same variable rslt_sum.
• Pass the given number divided by 10 as an argument to the chek_ArmstrngNumb function(which removes the last digit of the number) {Recursive Logic}.
• Return rslt_sum.
• Check if the chek_ArmstrngNumb which takes the given number as an argument is equal to the given number using the if conditional statement.
• If the statement is true, then print “The above-given number is an Armstrong Number”.
• Else print “The above-given number is not an Armstrong Number”.
• The Exit of the Program.

Below is the implementation:

# Create a recursive function to say chek_ArmstrngNumb which takes the given number as
# an argument and returns the rslt_sum.

def chek_ArmstrngNumb(gven_numb):
# Make the rslt_sum a global declaration.
global rslt_sum
# Check if the given number is not equal to 0 using the if conditional statement.
if (gven_numb != 0):
# If the statement is true, get the last digit of the given number using the modulus
# operator and store it in another variable k.
k = gven_numb % 10
# Calculate the value of k raised to power 3 using the pow() function and add it to the
# rslt_sum.
# Store it in the same variable rslt_sum.
rslt_sum += pow(k, 3)
# Pass the given number divided by 10 as an argument to the chek_ArmstrngNumb function
# (which removes the last digit of the number) {Recursive Logic}.

chek_ArmstrngNumb(gven_numb//10)
# Return rslt_sum.
return rslt_sum

# Give the number as user input using the int(input()) function and
# store it in a variable.
gven_numb = int(input("Enter some random number = "))
# Take a variable say rslt_sum and initialize its value to 0.
# Pass the given number as an argument to the chek_ArmstrngNumb function.
rslt_sum = 0
# Check if the chek_ArmstrngNumb which takes the given number as an argument is equal
# to the given number using the if conditional statement.
if (chek_ArmstrngNumb(gven_numb) == gven_numb):
# If the statement is true, then print "The above given number is an Armstrong Number".
print("The above given Number is an Armstrong Number.")
else:
# Else print "The above given number is not an Armstrong Number".
print("The above given Number is Not an Armstrong Number.")


Output:

Enter some random number = 100
The above given Number is Not an Armstrong Number.

Access the big list of Python Programming Code Examples with actual logical code asked in Programming and Coding Interviews for Python and stand out from the crowd.