Java Program to Find the Product of Two Numbers Using Recursion
In this tutorial, we will learn how to find the product of two numbers using a recursive function. A recursive function is a function that calls itself. But before moving further, if you are not familiar with the concept of nested if statements in java, then do check the article on the topic Conditional Statement in Java.
Input: Enter the number: 7
Enter the number: 4
Output: The product of the two numbers is 28.
Two Cases arises for the above scenario:
Case 1: When the values are Pre-defined
Case 2: When the values are User-defined
Let us look at the examples of how to find the product of two numbers using a recursive function.
Program 1: Find the Product of Two Numbers using Recursion
In this program, we will see how to find the product of two numbers using recursion with pre-defined values.
Algorithm:
- Start
- Declare two variables.
- Ask the user to initialize these variables.
- Call a recursive function to find the product of these two numbers.
- If the first number is less than the second number then swap the values.
- Recursively find the second number times the sum of the first number.
- If any of them becomes zero, then return zero.
- Display the result.
- Stop.
The below example demonstrates how to find the product of two numbers using recursion.
//Java Program to Find the Product of Two Numbers Using Recursion
import java.io.*;
import java.util.*;
public class Main
{
// recursive function to calculate the product of two numbers
static int findProduct(int num1, int num2)
{
// if x is less than y then swap the numbers
if (num1 < num2)
return findProduct(num2, num1);
// iteratively calculate y times sum of x
else if (num2 != 0)
return (num1 + findProduct(num1, num2 - 1));
// if any of the two numbers is zero return zero
else
return 0;
}
// Driver Code
public static void main (String[] args)
{
int num1 = 7;
System.out.println("The first entered number is: "+num1);
int num2 = 8;
System.out.println("The second entered number is: "+num2);
System.out.print("The product of the two numbers is ");
System.out.println(findProduct(num1, num2));
}
}
The first entered number is: 7
The second entered number is: 8
The product of the two numbers is 56
Program 2: Find the Product of Two Numbers using Recursion
In this program, we will see how to find the product of two numbers using recursion with user-defined values.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare two variables.
- Ask the user to initialize these variables.
- Call a recursive function to find the product of these two numbers.
- If the first number is less than the second number then swap the values.
- Recursively find the second number times the sum of the first number.
- In case if both the numbers are less than 0, return the product of their absolute values.
- If any of them becomes zero, then return zero.
- Display the result.
- Stop.
The below example demonstrates how to find the product of two numbers using recursion.
//Java Program to Find the Product of Two Numbers Using Recursion
import java.util.*;
public class Main
{
// recursive function to calculate the product of two numbers
static int findProduct(int num1, int num2)
{
if (num1 > 0 && num2 < 0)
{
return findProduct(num2, num1);
}
// case 2 : both num1 and num2 are less than 0
// return the product of their absolute values
else if (num1 < 0 && num2 < 0)
{
return findProduct((-1 * num1), (-1 * num2));
}
// if num1 > num2 , swap num1 and num2
if (num1 > num2)
{
return findProduct(num2, num1);
}
else if (num2 != 0)
{
return num1 + findProduct(num1, num2 - 1);
}
// num1=0 then return 0
else
{
return 0;
}
}
// Driver Code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first number");
int num1 = sc.nextInt();
System.out.println("Enter the second number");
int num2 = sc.nextInt();
System.out.print("The product of the two numbers is ");
System.out.println(findProduct(num1, num2));
}
}
Enter the first number: 4
Enter the second number: -5
The product of the two numbers is -20