In: Computer Science
For product using Recursion we use a simple rule that
Product x*y is nothing but adding 'x' to the sum 'y ' times
Source Code:
import java.util.Scanner;
class ex
{
static int recursiveProduct(int x,int y)
{
if(y<=1)
return x;
return
x+recursiveProduct(x,--y);
}
public static void main(String[] args) {
Scanner input=new
Scanner(System.in);
System.out.print("Enter number 1:
");
int num1=input.nextInt();
System.out.print("Enter number 2:
");
int num2=input.nextInt();
System.out.println("The product of
"+num1+" and "+num2+" is "+recursiveProduct(num1,num2));
}
}
sample input and output: