Write a program in Java to calculate multiplication of two numbers .
Answers
Answer:
hi there are three and note on decision to make sure
Multiplication - Java
We need to first take User Input to accept two numbers from the user. We will do this by using the Scanner class.
We store them as int type values. Then, we multiply the two numbers using the multiply operator and display it.
The Java Program
Here's the Java program to calculate the multiplication of two numbers:
import java.util.Scanner; // Importing Scanner
public class Multiplication { // Creaying Class
// The MAIN function
public static void main(String[] args) {
// Creating object of scanner
Scanner sc = new Scanner(System.in);
// Taking user input to accept the first number and storing in integer data type
System.out.print("Enter first number: ");
int a = sc.nextInt();
// Taking user input to accept the second number and storing in integer data type
System.out.print("Enter second number: ");
int b = sc.nextInt();
// Closing Scanner after the use
sc.close();
// Calculating product of two numbers
int product = a*b;
// Displaying the result
System.out.println("The multiplication of two given numbers is " + product);
}
}
Program Explanation
In the program, we have imported java.util.Scanner in our program.
Then, we have created an object of the Scanner class which will be used to take user input.
The parameter System.in is used to take input from the standard input.
We have used sc object to call the nextInt() method.
The method takes two integers input from the user and assigns them as a and b variables.
Notice the statement,
Here, the * operator multiplies two numbers and assigns the result to the product variable.
Finally, product is print on the screen using the println() method.