
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Product of Two Numbers Using Recursion in Java
In this article, we will understand how to find the product of two numbers using recursion in Java. 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.
Problem Statement
Write a program in Java to find the product of two numbers using recursion. Below is a demonstration of the same ?
Input
Enter two number : 12 and 9
Output
The product of 12 and 9 is 108
Different approaches
Following are the different approaches to find the product of two numbers using recursion ?
By using user input
Following are the steps to find the product of two numbers using recursion using user input ?
- Import the Scanner class from java.util package for reading user input.
- In the ProductRecursion class, create the main method where the user will input two numbers.
- Define a Scanner object (my_scanner) to read input from the console.
- Prompt the user to enter two numbers (my_input_1 and my_input_2), and store these in respective variables.
- Call the getproduct method, passing my_input_1 and my_input_2 as arguments.
- Define the Recursive Method getproduct:
- If my_input_1 is less than my_input_2, swap the arguments by calling getproduct again with swapped parameters.
- If my_input_2 is not zero, add my_input_1 to the result of getproduct(my_input_1, my_input_2 - 1).
- If my_input_2 is zero, return 0 (base case).
- Print the product of the two numbers on the console.
- Close the Scanner object to prevent resource leaks.
Example
Here, the user enters the input based on a prompt
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
By using predefined values
Following are the steps to find the product of two numbers using recursion predefined values ?
- In the ProductRecursion class, define the main method to calculate the product of predefined numbers.
- Set predefined values for my_input_1 and my_input_2.
- Print a message displaying the two defined numbers.
- Call the getproduct method, passing my_input_1 and my_input_2 as arguments.
- Define the Recursive Method getproduct:
- Check if my_input_1 is less than my_input_2. If true, call getproduct with swapped arguments.
- If my_input_2 is not zero, recursively add my_input_1 to the result of getproduct(my_input_1, my_input_2 - 1).
- If my_input_2 is zero, return 0 as the base case.
- Print the product of the two numbers on the console.
Example
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