Problem statement:- Program to Multiply two numbers using recursion.
Data requirement:-
Input Data:- num1, num2
Output Data:-Multiplication(num1,num2)
Output Data:-Multiplication(num1,num2)
Program in C
Here is the source code of the C Program to Multiply two numbers using recursion.
Code:
#include<stdio.h>
int Multiplication(int num1, int num2)
{
if(num1<num2)
return Multiplication(num2, num1);
else if(num2!=0)
return (num1+Multiplication(num1, num2-1));
else
return 0;
}
int main()
{
int num1, num2;
printf("Enter the two Number:");
scanf("%d%d",&num1,&num2);
printf("Multiplication of Two Number Using Recursion is:%d",Multiplication(num1,num2));
}
Enter the two Number:10
5
Multiplication of Two Number Using Recursion is:50
Program in C++
Here is the source code of the C++ Program to Multiply two numbers using recursion.
Code:
#include<iostream>
using namespace std;
int Multiplication(int num1, int num2)
{
if(num1<num2)
return Multiplication(num2, num1);
else if(num2!=0)
return (num1+Multiplication(num1, num2-1));
else
return 0;
}
int main()
{
int num1, num2;
cout<<"Enter the two Number:";
cin>>num1>>num2;
cout<<"Multiplication of Two Number Using Recursion is:"<<Multiplication(num1,num2);
}
Enter the two Number:10
13
Multiplication of Two Number Using Recursion is:130
Program in Java
Here is the source code of the Java Program to Multiply two numbers using recursion.
Code:
import java.util.Scanner;
public class MultiplicationOfTwoNumberUsingRecursion {
static int Multiplication(int num1, int num2)
{
if(num1<num2)
return Multiplication(num2, num1);
else if(num2!=0)
return (num1+Multiplication(num1, num2-1));
else
return 0;
}
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("Multiplication of Two Number Using Recursion is: "+Multiplication(num1,num2));
cs.close();
}
}
Enter the two Number:13
3
Multiplication of Two Number Using Recursion is: 39
Program in Python
Here is the source code of the Python Program to Multiply two numbers using recursion.
Code:
def Multiplication(num1,num2):
if num1<num2:
return Multiplication(num2, num1)
elif num2!=0:
return num1 + Multiplication(num1, num2 - 1)
else:
return 0
print("Enter the two Number:")
num1=int(input())
num2=int(input())
print("Multiplication of Two Number Using Recursion is: ",Multiplication(num1,num2))
Enter the two Number:
303
15
Multiplication of Two Number Using Recursion is: 4545
More:-
0 Comments
Please do not Enter any spam link in the comment box