Java program to find product of two numbers using recursive function

Java program to find product of two numbers using recursive function. This is a Java program that uses recursion to find the product of two numbers. As an input, enter two integer numbers. Then we feed the two specified numbers, along with zero, to the other function, which uses recursion to get numbers one by one to multiply. As a result, the output is the product of the two provided numbers.

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
int[] a = new int[2];
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number");
a[0] = s.nextInt();
System.out.print("Enter the second number");
a[1] = s.nextInt();
Main obj = new Main();
int mul = obj.multiply(a,0);
System.out.println("Answer:"+mul);
}
int multiply(int x[], int i)
{
if(i < 2)
{
return x[i] * multiply(x, ++i);
}
else
{
return 1;
}
}
}

 

Output:

Enter the first number 45
Enter the second number 56
Answer: 2520

Recursion method in java program

In Java, recursion is the process of a method constantly calling itself. In Java, a recursive method refers to a method that calls itself. It compresses the code but makes it difficult to comprehend. When a recursive function calls itself, the called function’s memory is allocated on top of the calling function’s memory, and each function call creates a new copy of local variables

nextInt() method in java program

nextInt() is a method that returns an integer. As an int, scans the next token of the input. Invocations of this function with the form nextInt() behave precisely the same as invocations with the form nextInt(radix), where radix is the scanner’s default radix.

What is conditional operator in java program

Conditional statements assist you in making decision depending on set of circumstances. collection of conditional statements using boolean expressions that evaluate to true or false are used to specify these conditions.