Python Program to Divide Two Numbers Using Recursion

In the previous article, we have discussed Python Program to Multiply Two Numbers Using Recursion

Given two numbers and the task is to find the division of the given two numbers 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 First Number      =  18
Given Second Number =  9

Output:

The Division of { 18 / 9 } using recursion = 2

Example2:

Input:

Given First Number      = 48
Given Second Number = 6

Output:

The Division of { 48 / 6 } using recursion = 8

Program to Divide Two Numbers Using Recursion in Python

Below are the ways to find the division of the given two numbers using recursion :

Method #1: Using Recursion (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to recur_div function.
  • Create a recursive function to say recur_div which takes the two numbers as arguments and returns the division of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return 0.
  • Return  1 + recur_div(fst_numb-secnd_numb, secnd_numb) {Recursive Logic}.
  • Print the division of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_div which takes the two numbers as arguments
# and returns the division of the given two numbers using recursion.


def recur_div(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return 0
        return 0
  # Return  1 + recur_div(fst_numb-secnd_numb, secnd_numb) {Recursive Logic}.
    return 1 + recur_div(fst_numb-secnd_numb, secnd_numb)


# Give the first number as static input and store it in a variable.
fst_numb = 18
# Give the second number as static input and store it in another variable.
secnd_numb = 9
# Pass the given two numbers as the arguments to recur_div function.
# Print the division of given two numbers using recursion.
print("The Division of {", fst_numb, "/", secnd_numb,
      "} using recursion =", recur_div(fst_numb, secnd_numb))

Output:

The Division of { 18 / 9 } using recursion = 2

Method #2: Using Recursion (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to the recur_div function.
  • Create a recursive function to say recur_div which takes the two numbers as arguments and returns the division of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return 0.
  • Return  1 + recur_div(fst_numb-secnd_numb, secnd_numb) {Recursive Logic}.
  • Print the division of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_div which takes the two numbers as arguments
# and returns the division of the given two numbers using recursion.


def recur_div(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return 0
        return 0
  # Return  1 + recur_div(fst_numb-secnd_numb, secnd_numb) {Recursive Logic}.
    return 1 + recur_div(fst_numb-secnd_numb, secnd_numb)


# Give the first number as user input using the int(input()) function and 
# store it in a variable.
fst_numb = int(input("Enter some random number = "))
#Give the second number as user input using the int(input()) function and 
# store it in another variable.
secnd_numb = int(input("Enter some random number = "))
# Pass the given two numbers as the arguments to recur_div function.
# Print the division of given two numbers using recursion.
print("The Division of {", fst_numb, "/", secnd_numb,
      "} using recursion =", recur_div(fst_numb, secnd_numb))

Output:

Enter some random number = 45
Enter some random number = 9
The Division of { 45 / 9 } using recursion = 5

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.