In this tutorial, we will discuss the concept of Find the product of two numbers in Java using recursion
In this article, we are going to learn how to Find the product of two numbers using recursion in the Java programming language
Program
This program allows the entry of two digits from the user and to find the product of two numbers using the recursive function in Java programming language.
import java.util.Scanner; class ProductOfTwoNumRec{ public static void main(String args[]){ int sum=0; //variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter two numbers to calculate product: "); int num1=scan.nextInt(); int num2=scan.nextInt(); int result=product(num1,num2);//assign the output to variable result //function call System.out.print("Product of "+num1+" and "+num2+" is :"+result); } static int product(int a, int b) { if(a<b) { return product(b,a); } else if(b!=0){ return (a+product(a,b-1)); } else{ return 0; } } }
When the above code is executed, it produces the following results
Enter two numbers two calculate product: 12 23 Product of 12 and 23 is: 276
Method
Similar post
Calculate the product of two numbers in C++ using recursion
Calculate the product of two numbers in C using recursion
Calculate the product of two numbers in Python using recursion
Suggested for you
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…