# Subtraction in python – Python Program to Find Subtraction of Two Numbers using Recursion

Subtraction in python: In the previous article, we have discussed Python Program to Find Sum of Two Numbers using Recursion

Given two numbers and the task is to find the subtraction 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      =  10
Given Second Number =   4

Output:

The subtraction of { 10 - 4 } using recursion = 6

Example2:

Input:

Given First Number      = 35
Given Second Number = 12

Output:

The subtraction of { 35 - 12 } using recursion = 23

## Program to Find Subtraction of Two Numbers using Recursion in Python

Below are the ways to find the subtraction 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_sub function
• Create a recursive function to say recur_sub which takes the two numbers as arguments and returns the subtraction of the given two numbers using recursion.
• Check if the second number is equal to 0 using the if conditional statement.
• If the statement is true, return the first number.
• Else return (fst_numb-1, secnd_numb-1) {Recursive logic}
• Print the subtraction of given two numbers using recursion.
• The Exit of the Program.

Below is the implementation:

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

def recur_sub(fst_numb, secnd_numb):
# Check if the second number is equal to 0 using the if conditional statement.
if secnd_numb == 0:
# If the statement is true, return the first number.
return fst_numb
# Else return (fst_numb-1, secnd_numb-1) {Recursive logic}
return recur_sub(fst_numb-1, secnd_numb-1)

# Give the first number as static input and store it in a variable.
fst_numb = 10
# Give the second number as static input and store it in another variable.
secnd_numb = 4
#Pass the given two numbers as the arguments to recur_sub function
# Print the subtraction of given two numbers using recursion.
print("The subtraction of {", fst_numb, "-", secnd_numb,
"} using recursion =", recur_sub(fst_numb, secnd_numb))


Output:

The subtraction of { 10 - 4 } using recursion = 6

### 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 recur_sub function
• Create a recursive function to say recur_sub which takes the two numbers as arguments and returns the subtraction of the given two numbers using recursion.
• Check if the second number is equal to 0 using the if conditional statement.
• If the statement is true, return the first number.
• Else return (fst_numb-1, secnd_numb-1) {Recursive logic}
• Print the subtraction of given two numbers using recursion.
• The Exit of the Program.

Below is the implementation:

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

def recur_sub(fst_numb, secnd_numb):
# Check if the second number is equal to 0 using the if conditional statement.
if secnd_numb == 0:
# If the statement is true, return the first number.
return fst_numb
# Else return (fst_numb-1, secnd_numb-1) {Recursive logic}
return recur_sub(fst_numb-1, secnd_numb-1)

# 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_sub function
# Print the subtraction of given two numbers using recursion.
print("The subtraction of {", fst_numb, "-", secnd_numb,
"} using recursion =", recur_sub(fst_numb, secnd_numb))


Output:

Enter some random number = 35
Enter some random number = 10
The subtraction of { 35 - 10 } using recursion = 25

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.