Find elements

Find the product of two numbers using recursion in Java

Find the product of two numbers using recursion in Java

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

The product of two numbers

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

  • Declare three variables num1, num2 and result as “int” type.
  • Receive two numbers from user and store variable as num1 and num2 respectively.
  • Call the function and assign the output value to variable result.
  • Product() function is used to Calculate  the product of of two numbers.
  • Display the result on the screen.

 

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

Method n Java language

Recursion in Java language

If statement in Java language

 

 

 

Find the product of two numbers using recursion in C++
Java program to compute the sum of digits in a given numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago