Add Two Numbers Using Recursive Function
This code defines a recursive function to add two numbers by incrementing one number and decrementing the other. User inputs two numbers, and the sum is calculated and displayed.
Python3
# Define a recursive function to add two numbers def add_numbers_recursive(x, y): if y = = 0 : return x else : return add_numbers_recursive(x + 1 , y - 1 ) # Take input from the user num1 = 1 num2 = 2 # Call the recursive function to add the two numbers result = add_numbers_recursive(num1, num2) # Print the result print ( "The sum of" , num1, "and" , num2, "is" , result) |
Output:
The sum of 1 and 2 is 3
We have covered multiple programs to add two number in Python. You can use any one of the above method, to perform the task. Adding two numbers is a very easy task in Python, can be done using multiple functionalities.
This tutorial showed you the basic of addition of two numbers in Python. You can be creative and customize these programs to add multiple numbers in Python.
You can also check other operations:
How to Add Two Numbers in Python – Easy Programs
Learn easy techniques and codes to add numbers in Python. Adding numbers in Python is a very easy task, and we have provided you 7 different ways to add numbers in Python.
Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers.
Examples:
Input: num1 = 5, num2 = 3
Output: 8
Input: num1 = 13, num2 = 6
Output: 19
There are many methods to add two number in Python:
- Using “+” operator
- Defining a function that adds two number
- Using operator.add method
- Using lambda function
- Using Recursive function
Each of these methods have been explained with their program below.