In this article we are going to see how we can multiply two numbers using recursion by Java programming language.
Java Program to Multiply Two Numbers Using Recursion
Multiplying two numbers means finding the product of two numbers.
Let’s see the program how we you multiply two numbers using recursion.
- Java Program to Multiply Two Numbers Using Recursion By Using Static Input Value
- Java Program to Multiply Two Numbers Using Recursion By Using User Input Value
Method-1: Java Program to Multiply Two Numbers Using Recursion By Using Static Input Value
Approach:
- Store two numbers in two variables.
- Call the user defined method
mulProd( )
to find the product and store it. The methodprod()
ensures that num1>num2 else swaps them. Then when num2 is greater than zero it callsmulProd( )
on num1 and decremented num2 then adds the result with num1. - Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to multiply two numbers public static int mulProd(int num1, int num2) { // If num2 is greater than num1 swap them and call the function if (num1 < num2) { return mulProd(num2, num1); } // If num2 is greater than 0 then call the function by decrementing num2 by 1 and add the current num1 value to the result. else if (num2 != 0) { return (num1 + mulProd(num1, num2 - 1)); } else { return 0; } } public static void main(String[] args) { int num1 = 29, num2 = 10; // Call the method and store the result int prod = mulProd(num1,num2); // Print the result System.out.println("The product of "+num1+" and "+num2+" is "+prod); } }
Output: The product of 29 and 10 is 290
Method-2: Java Program to Multiply Two Numbers Using Recursion By Using User Input Value
Approach:
- Ask the user to enter two numbers in order.
- Store two numbers in two variables.
- Call the user defined method
mulProd( )
to find the product and store it. The methodprod()
ensures that num1>num2 else swaps them. Then when num2 is greater than zero it callsmulProd( )
on num1 and decremented num2 then adds the result with num1. - Print the result.
Program:
import java.util.*; // Main class public class Main { // Recursive method to multiply two numbers public static int mulProd(int num1, int num2) { // If num2 is greater than num1 swap them and call the function if (num1 < num2) { return mulProd(num2, num1); } // If num2 is greater than 0 then call the function by decrementing num2 by 1 and add the current num1 value to the result. else if (num2 != 0) { return (num1 + mulProd(num1, num2 - 1)); } else { return 0; } } public static void main(String[] args) { // Taking user input Scanner sc = new Scanner(System.in); // Ask the user to enter two numbers System.out.print("Enter two numbers to multiply "); int num1 = sc.nextInt(), num2 = sc.nextInt(); // Call the method and store the result int prod = mulProd(num1,num2); // Print the result System.out.println("The product of "+num1+" and "+num2+" is "+prod); } }
Output: Enter two numbers to multiply 15 50 The product of 15 and 50 is 750
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.