Computer Science, asked by shwetathapar11, 6 days ago

Write a program in Java to calculate multiplication of two numbers .​

Answers

Answered by sammipanwar85
0

Answer:

hi there are three and note on decision to make sure

Answered by Anonymous
2

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.

\tt{import \; java.util.Scanner;}

Then, we have created an object of the Scanner class which will be used to take user input.

\texttt{// Creating object of scanner}

\texttt{Scanner sc = new Scanner(System.in);}

The parameter System.in is used to take input from the standard input.

We have used sc object to call the nextInt() method.

\texttt{int a = sc.nextInt();}

\texttt{int b = sc.nextInt();}

The method takes two integers input from the user and assigns them as a and b variables.

Notice the statement,

\texttt{int product = a*b;}

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.

\texttt{System.out.println("The multiplication of two given numbers is " + product);}

Attachments:
Similar questions