Sum of Two Numbers Using Recursion Java

Find the product of two numbers using recursion in java. 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;
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number");
a = s.nextInt();
System.out.print("Enter the second number");
a = s.nextInt();
Main obj = new Main();
int mul = obj.multiply(a,0);
}
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

Recursion Method in Java

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

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.

Conditional Operator in Java

Conditional statements assist you in making a decision depending on a set of circumstances.

A collection of conditional statements using boolean expressions that evaluate to true or false are used to specify these condition.