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
add
to 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
a
andb
.
- Ask the user to enter two positive integers
- Calculate the sum of the two numbers:
- Call the
add
function witha
andb
as 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 8
Explanation
-
We define a recursive function
add
to add two positive numbers:- If the second number
b
is 0, the function returns the first numbera
because adding 0 to any number results in the number itself. - Otherwise, the function returns
1
plus the result of addinga
andb-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
5
and3
. - The function call
add(5, 3)
will execute as follows:add(5, 3)
:- Since
3
is not 0, it returns1 + add(5, 2)
.
- Since
add(5, 2)
:- Since
2
is not 0, it returns1 + add(5, 1)
.
- Since
add(5, 1)
:- Since
1
is not 0, it returns1 + add(5, 0)
.
- Since
add(5, 0)
:- Since
0
is 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
5
and3
is8
. -
We take two input numbers
a
andb
from the user and store them in the variablesa
andb
. -
We calculate the sum of
a
andb
by calling theadd
function witha
andb
as 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 makesb
recursive calls. - The space complexity of the recursive addition function is
O(b)
because it uses the call stack to storeb
recursive 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.