Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Python program to Multiply two integers|in 5ways

Find product of two numbers in Java language|5ways

Posted on September 3, 2020September 6, 2020

Table of Contents

  • Find product of two numbers in Java language|5ways
    • Product of two numbers in Java
      • Product of two numbers in Java language-standard method
      • Product of two numbers in Java language-using user input
      • Product of two numbers in Java language-using method
      • Product of two numbers in Java language-using recursion
      • Product of two numbers in Java language-with out * operator

Find product of two numbers in Java language|5ways

In this tutorial, we will discuss the Find product of two numbers in Java language

In this post, we are going to learn how to find multiplication of two numbers via different 5 ways in Java

Product of two numbers in Java

Product of two numbers in Java language-standard method

Program 1

public class FindProduct{
public static void main(String args[]){
int num1=12,num2=15;
int mul=num1*num2
System.out.println("Product of "+num1+" and "+num2+":"+mul);
}
}

When the above code is executed,  it produces the following result

Product of 12 and 15: 180

 

In this program,

  • integer variables num1,num2 are declared and initialized
  • The num1 and num2 both are multiplied together and the value is assigned to the variable mul
  • Then,the program is displayed the output using System.out.println();

 

Product of two numbers in Java language-using user input

Program 2

import java.util.Scanner;
public class FindProduct1{
public static void main(String args[]){
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the value to num1: ");
int num1=scan.nextInt();
System.out.print("Enter the value to num2: ");
int num2=scan.nextInt();
int mul=num1*num2;
System.out.println("Product of "+num1+" and "+num2+":"+mul);
}
}

When the above code is executed,  it produces the following result

Enter the value to num1:25
Enter the value to num2:50
Product of 25 and 50: 1250

 

In the program,

  • Declare variables num1, num2, mul
  • The program asks input from the user
  • Then the user enters the input values for num1 and num2.
  • The program will read the input using scanner class and stores the variables num1 and num2 respectively
  • The num1 and num2 both are multiplied together and the value assigned to the mul variable
  • Then, the program displays the output using System.out.println() function

 

 

Product of two numbers in Java language-using method

Program 3

import java.util.Scanner;
public class FindProduct2{
static void productNum(int x,int y){//user defind method
int product=x*y;//caculate product of two numbers
System.out.println("Product of "+x+" and "+y+":"+product);
//display result

}
public static void main(String args[]){//main method
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the value to num1: ");
int num1=scan.nextInt();
System.out.print("Enter the value to num2: ");
int num2=scan.nextInt();
productNum(num1,num2);//calling the method


}

}

When the above code is executed,  it produces the following result

Enter the value to num1:20
Enter the value to num2:15
Product of 20 and 15: 300

 

In the program,

  • Declare variables num1, num2.
  • The program asks input from the user
  • Then the user enters the input values for num1 and num2.
  • The program will read the input using scanner class and store the variables num1 and num2 respectively
  • Define the method(productNum()) for find product of given two values
  • Call the method to produce output
  • finally, the program displays the value of multiplication using System.out.println() function

 

Product of two numbers in Java language-using recursion

Program 4

import java.util.Scanner;
public class FindProduct3{

public static void main(String args[]){//main method
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the value to num1: ");
int num1=scan.nextInt();
System.out.print("Enter the value to num2: ");
int num2=scan.nextInt();
int result=productNum(num1,num2);//calling the method
System.out.println("Product of "+num1+" and "+num2+":"+result);

}
static int productNum(int x,int y){//user defind method
if(x<y)
{
  return productNum(y,x);
}
else if(y!=0){
  return(x+productNum(x,y-1));
}
else{
  return 0;
}
}

}

When the above code is executed,  it produces the following result

Enter the value to num1:50
Enter the value to num2:100
Product of 50 and 100: 5000


In the program,

  • Declare variables num1, num2.
  • The program asks input from the user
  • Then the user enters the input values for num1, num2.
  • The program will read the input using scanner class and store the variables num1 and num2 respectively
  • Define the method(productNum()) for find multiplication, recursively
  • Call the recursive method to produced output
  • finally, the program displays the output using System.out.println() function

 

Product of two numbers in Java language-with out * operator

Program 5

import java.util.Scanner;
public class FindProduct5{

public static void main(String args[]){//main method
int count=0,result=0;
Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the value to num1: ");
int num1=scan.nextInt();
System.out.print("Enter the value to num2: ");
int num2=scan.nextInt();
int product=0;
for(int i=0; i<num2; i++){
  product=product+num1;
}
System.out.print("The product of "+num1+" and "+num2+" is :"+product);
}
}

 

When the above code is executed,  it produces the following result

Enter the value to num1:200
Enter the value to num2:25
Product of 200 and 25 IS: 5000


Suggested post

Data type and variable in Java language

variable types in Java language

Operator in Java language

if statement in Java language

For loop in Java language

Method in Java language

Recursion in Java language

 

Similar post

Find product of two numbers in C language|6ways

Find product of two numbers in C++ language|6ways

Find product of two numbers in Python language|4ways

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes