Java Program to Find the Product of Two Numbers Using Recursion

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to find the product of two numbers using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

Input

Suppose our input is −

Enter two number : 12 and 9

Output

The desired output would be

The product of 12 and 9 is 108

Algorithm

Step 1 - START
Step 2 – Declare two integer values namely my_input and my_result
Step 3 - Read the required values from the user/ define the values
Step 4 - A recursive function ‘getproduct’ is defined which takes two integers as input. The function computes the reminder by re-iterating over the function multiple times, until the base condition is reached.
Step 5 - The recursive function ‘getproduct’ is called and its result is stored
Step 6 - Display the result
Step 7 - Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      System.out.println("Required packages have been imported");
      Scanner my_scanner = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.print("Enter the number : ");
      my_input_1 = my_scanner.nextInt();
      System.out.print("Enter the number : ");
      my_input_2 = my_scanner.nextInt();
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
          return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

Output

Required packages have been imported
A reader object has been defined
Enter the number : 12
Enter the number : 9
The product of 12 and 9 is 108

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class ProductRecursion{
   public static void main (String[] args){
      int my_input_1, my_input_2;
      my_input_1 = 12;
      my_input_2 = 9;
      System.out.println("The two numbers are defined as " +my_input_1 +" and " +my_input_2);
      System.out.println("The product of "+my_input_1 +" and " +my_input_2 +" is " +getproduct(my_input_1, my_input_2));
   }
   static int getproduct(int my_input_1, int my_input_2){
      if (my_input_1 < my_input_2)
         return getproduct(my_input_2, my_input_1);
      else if (my_input_2 != 0)
         return (my_input_1 + getproduct(my_input_1, my_input_2 - 1));
      else
         return 0;
   }
}

Output

The two numbers are defined as 12 and 9
The product of 12 and 9 is 108
raja
Updated on 22-Feb-2022 10:31:13

Advertisements