Recursive Function to Add Two Positive Numbers
Aim
Write a program to add two positive numbers using recursion in Python.
- Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.
- Adding two numbers using recursion involves breaking down the addition process into smaller steps until a base case is reached.
Algorithm
- Start
- Define a recursive function
addto add two positive numbers:- If the second number is 0, return the first number (base case).
- Otherwise, return 1 plus the result of adding the first number and the second number minus 1 (recursive case).
- Take input from the user:
- Ask the user to enter two positive integers
aandb.
- Ask the user to enter two positive integers
- Calculate the sum of the two numbers:
- Call the
addfunction withaandbas arguments and store the result.
- Call the
- Print the result:
- Display the sum of the two numbers.
- End
Program
# Define a recursive function to add two positive numbers
def add(a, b):
if b == 0:
return a
else:
return 1 + add(a, b - 1)
# Take input from the user
a = int(input("Enter the first positive integer: "))
b = int(input("Enter the second positive integer: "))
# Calculate the sum of the two numbers
result = add(a, b)
# Print the result
print(f"The sum of {a} and {b} is {result}")Sample Input/Output
Enter the first positive integer: 5
Enter the second positive integer: 3
The sum of 5 and 3 is 8Explanation
-
We define a recursive function
addto add two positive numbers:- If the second number
bis 0, the function returns the first numberabecause adding 0 to any number results in the number itself. - Otherwise, the function returns
1plus the result of addingaandb-1, which is the recursive call.
- If the second number
-
Let's break down the execution of the recursive function with an example:
- Suppose the user enters
5and3. - The function call
add(5, 3)will execute as follows:add(5, 3):- Since
3is not 0, it returns1 + add(5, 2).
- Since
add(5, 2):- Since
2is not 0, it returns1 + add(5, 1).
- Since
add(5, 1):- Since
1is not 0, it returns1 + add(5, 0).
- Since
add(5, 0):- Since
0is 0, it returns5.
- Since
- Suppose the user enters
-
Now, let's substitute back the values:
add(5, 0)returns5.add(5, 1)returns1 + 5 = 6.add(5, 2)returns1 + 6 = 7.add(5, 3)returns1 + 7 = 8.
-
Therefore, the sum of
5and3is8. -
We take two input numbers
aandbfrom the user and store them in the variablesaandb. -
We calculate the sum of
aandbby calling theaddfunction withaandbas arguments and store the result in the variableresult. -
We print the result using the statement
print(f"The sum of {a} and {b} is {result}").
Complexity Analysis
- The time complexity of the recursive addition function is
O(b)because it makesbrecursive calls. - The space complexity of the recursive addition function is
O(b)because it uses the call stack to storebrecursive calls.
How can you improve this program?
- You can add error handling to ensure the user enters positive integers.
- You can implement an iterative version of the addition function to avoid the overhead of recursive calls.
Summary
In this tutorial, we learned how to add two positive numbers using recursion in Python. We discussed the algorithm, program, and sample input/output for adding two numbers. We also analyzed the complexity of the recursive addition function and suggested possible improvements.