Table of Contents
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
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,
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,
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,
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,
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.