Java Program to Find Product of Two Numbers using Recursion

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.

  1. import java.util.Scanner;
  2. public class Multiply_Recursion 
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int[] a = new int[2];
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter the first number:");
  9.         a[0] = s.nextInt();
  10.         System.out.print("Enter the second number:");
  11.         a[1] = s.nextInt();
  12.         Multiply_Recursion obj = new Multiply_Recursion();
  13.         int mul = obj.multiply(a,0);
  14.         System.out.println("Answer:"+mul);
  15.     } 
  16.    int multiply(int x[], int i)
  17.     {
  18.         if(i < 2)
  19.         {
  20.             return x[i] * multiply(x, ++i);
  21.         }
  22.         else
  23.         {
  24.             return 1;
  25.         }
  26.     }
  27. }

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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.