Java program to multiply two numbers using method
- Home
- Calculations
- Java program to multiply two numbers using method
- On
- By
- 0 Comment
- Categories: Calculations, multiply
Java program to multiply two numbers using method
Java program to multiply two numbers using the method
In this program, we will discuss the Java program to multiply two numbers using the method
In this topic, we will learn a simple concept of how to multiply two number in Java programming language using the Java method.
already we knew the same concept using the Java operator.
if you want to know about that, click here Java program to multiply two numbers
Program 1
the following programs have the following 4 different steps to completion
- Get input from the user for num1,num2- Using Scanner class
- Declare the variable to store the value
- create the method with the return value
- Calling the method
Java program to multiply two integers using the method
import java.util.Scanner; public class ProductOfNumbur1{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); int num1=sc.nextInt(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); int num2=sc.nextInt(); //this method reads value for num2 providing by user sc.close();//closing the scanner class calcProduct(num1,num2); //calling the method } //calcTotal method public static void calcProduct(int x,int y){ int result=0; result=x*y; System.out.println("product of two numbers "+result); } }
When the above code is compiled and executed, it produces the following results
Enter the first number: 10 Enter the second number: 33 product of two numbers 330
In this program, the user is asked to enter two numbers(integer numbers). then it calculating product of those numbers using Java method and display on the screen.
Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.
Program 2
Java program to multiply two integers using the method with return
This is a program to calculate product of floating point values using Java method
import java.util.Scanner; public class ProductOfNumbur{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); int num1=sc.nextInt(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); int num2=sc.nextInt(); //this method reads value for num2 providing by user sc.close();//closing the scanner class int Product=calcProduct(num1,num2); //calling the method System.out.println("product of two numbers "+Product); } //calcProduct method public static int calcProduct(int x,int y){ int result=x*y; return result;//returning the result to main method } }
When the above code is compiled and executed, it produces the following results
Enter the first number: 12 Enter the second number: 23 product of two numbers 276
Program 3
Java program to multiply two floating numbers using the method
This is a program to calculate product of floating point values
import java.util.Scanner; public class ProductOfNumbur2{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); // create scanner object System.out.print("Enter the first number: "); double num1=sc.nextDouble(); //this method reads value for num1 providing by user System.out.print("Enter the second number: "); double num2=sc.nextDouble(); //this method reads value for num2 providing by user sc.close();//closing the scanner class calcProduct(num1,num2); //calling the method } //calcTotal method public static void calcProduct(double x,double y){ double result=x*y; System.out.println("product of two numbers "+result); } }
When the above code is compiled and executed, it produces the following results
Enter the first number:32.21 Enter the second number:12.1 Product of two numbers 389.741
In this program, the user is asked to enter two numbers(floating numbers). then it calculating product of those numbers using Java method and display on the screen.
Here, when the user enters the numbers, the first number stored in variable num1 and the second number stored the variable num2. Then, the product of the numbers is stored in the variable product.
Suggested for you
The data type in the Java language