Python Program to Find Sum of Two Numbers using Recursion

In the previous article, we have discussed Python Program to Find the First Small Letter in a Given String

Given two numbers and the task is to find the sum 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      =  3
Given Second Number =  0

Output:

The sum of { 3 } and { 0 } using recursion = 3

Example2:

Input:

Given First Number      = 20
Given Second Number = 30

Output:

The sum of { 20 } and { 30 } using recursion = 50

Program to Find Sum of Two Numbers using Recursion in Python

Below are the ways to find the sum 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_sum function
  • Create a recursive function to say recur_sum which takes the two numbers as arguments and returns the sum 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, secnd_numb-1)+1 {Recursive logic}.
  • Print the sum of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

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


def recur_sum(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, secnd_numb-1)+1 {Recursive logic}
    return recur_sum(fst_numb, secnd_numb-1)+1


# Give the first number as static input and store it in a variable.
fst_numb = 3
# Give the second number as static input and store it in another variable.
secnd_numb = 0
#Pass the given two numbers as the arguments to recur_sum function
print("The sum of {", fst_numb, "} and {", secnd_numb,
      "} using recursion =", recur_sum(fst_numb, secnd_numb))

Output:

The sum of { 3 } and { 0 } using recursion = 3

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_sum function
  • Create a recursive function to say recur_sum which takes the two numbers as arguments and returns the sum 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, secnd_numb-1)+1 {Recursive logic}.
  • Print the sum of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

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


def recur_sum(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, secnd_numb-1)+1 {Recursive logic}
    return recur_sum(fst_numb, secnd_numb-1)+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_sum function
print("The sum of {", fst_numb, "} and {", secnd_numb,
      "} using recursion =", recur_sum(fst_numb, secnd_numb))

Output:

Enter some random number = 20
Enter some random number = 30
The sum of { 20 } and { 30 } using recursion = 50

Practice Python Program Examples to master coding skills and learn the fundamental concepts in the dynamic programming language Python.