Problem statement:- Program to Find the sum of two numbers using recursion.
Data requirement:-
Input Data:- num1, num2
Output Data:-sum(num1,num2)
Output Data:-sum(num1,num2)
Program in C
Here is the source code of the C Program to Find the sum of two numbers using recursion.
Code:
#include<stdio.h>
int sum(int num1, int num2)
{
if(num2==0)
return num1;
return sum(num1, num2-1)+1;
}
int main()
{
int num1, num2;
printf("Enter the two Number:");
scanf("%d%d",&num1,&num2);
printf("Sum of Two Number Using Recursion is:%d",sum(num1,num2));
}
Enter the two Number:10
25
Sum of Two Number Using Recursion is:35
Program in C++
Here is the source code of the C++ Program to Find the sum of two numbers using recursion.
Code:
#include<iostream>
using namespace std;
int sum(int num1, int num2)
{
if(num2==0)
return num1;
return sum(num1, num2-1)+1;
}
int main()
{
int num1, num2;
cout<<"Enter the two Number:";
cin>>num1>>num2;
cout<<"Sum of Two Number Using Recursion is:"<<sum(num1,num2);
}
Enter the two Number:24
36
Sum of Two Number Using Recursion is:60
Program in Java
Here is the source code of the Java Program to Find the sum of two numbers using recursion.
Code:
import java.util.Scanner;
public class SumOfTwoNumberUsingRecursion {
static int sum(int num1, int num2)
{
if(num2==0)
return num1;
return sum(num1, num2-1)+1;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int num1, num2;
System.out.print("Enter the two Number:");
num1=cs.nextInt();
num2=cs.nextInt();
System.out.print("Sum of Two Number Using Recursion is: "+sum(num1,num2));
cs.close();
}
}
Enter the two Number:25
30
Sum of Two Number Using Recursion is: 55
Program in Python
Here is the source code of the Python Program to Find the sum of two numbers using recursion.
Code:
def sum(num1,num2):
if num2==0:
return num1
return sum(num1, num2-1)+1
print("Enter the two Number:")
num1=int(input())
num2=int(input())
print("Sum of Two Number Using Recursion is: ",sum(num1,num2))
Enter the two Number:
55
100
Sum of Two Number Using Recursion is: 155
Most Recommend Questions:-
More:-
0 Comments
Please do not Enter any spam link in the comment box