Python Program to add two number using Recursion

In this tutorial, we will learn to write Python programs to add two numbers using Recursion. Recursion is a powerful programming technique in which a function calls itself again and again.

For Examples

Suppose we have two number num1 = 5 num2 = 7,

Our Output will be 12.

Add two numbers using Recursion

To add two numbers using recursion, We will write a recursive function that will take two arguments, num1, and num2. Our base case for the recursion will be when num2 becomes 0. It means we have added all the numbers. Below is the program

def add(num1, num2):
    if num2 == 0:
        return num1
    else:
        return add(num1 ^ num2, (num1 & num2) << 1)
num1 = 5
num2 = 7
result = add(num1, num2)
print("The sum of", num1, "and", num2, "is", result)

Output:

The sum of 5 and 7 is 12

Conclusion:

In this post, we have learned to write a Python program to add two numbers using recursion in Python. Hope this problem is helpful to you.