Table of Contents
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
Recursion in Java language
Similar post
Find product of two numbers in C language|6ways