This is a Java Program to Find Product of 2 Numbers using Recursion.
Enter two integer numbers as an input. After that we pass the two given numbers along with zero to the other function where get numbers one by one to multiply with the help of recursion. Hence we get the product of the two given numbers as an output.
Here is the source code of the Java Program to Find Product of 2 Numbers using Recursion. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Multiply_Recursion
{
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();
Multiply_Recursion obj = new Multiply_Recursion();
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:
$ javac Multiply_Recursion.java $ java Multiply_Recursion Enter the first number:6 Enter the second number:4 Answer:24
Sanfoundry Global Education & Learning Series - 1000 Java Programs.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Computer Science Internship
- Check Programming Books
- Check Java Books
- Apply for Java Internship
- Practice Programming MCQs