Java Program To Multiply Two Number

This program asks user to enter two integer numbers and displays the product. To understand how to use scanner to take user input, checkout this program: Program to read integer from system input.

Example 1: Java program to multiplication of integar numbers

  import java.util.Scanner;

 class Demo {

    public static void main(String[] args) {

        /* This reads the input provided by user
         * using keyboard
         */
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first number: ");

        // This method reads the number provided using keyboard
        int num1 = scan.nextInt();

        System.out.print("Enter second number: ");
        int num2 = scan.nextInt();

        // Closing Scanner after the use
        scan.close();

        // Calculating product of two numbers
        int product = num1*num2;

        // Displaying the multiplication result
        System.out.println("Output: "+product);
    }
}
      
Output:
Enter first number: 12
Enter second number: 55
Output: 660

Example 2: Floating point numbers and display the multiplication

if we want to calculate the multiplication of two float numbers? This programs allows you to enter float numbers and calculates the product.
Here we are using data type double for numbers so that you can enter integer as well as floating point numbers.
    import java.util.Scanner;

 class Demo {

    public static void main(String[] args) {

        /* This reads the input provided by user
         * using keyboard
         */
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first number: ");

        // This method reads the number provided using keyboard
        double num1 = scan.nextDouble();

        System.out.print("Enter second number: ");
        double num2 = scan.nextDouble();

        // Closing Scanner after the use
        scan.close();

        // Calculating product of two numbers
        double product = num1*num2;

        // Displaying the multiplication result
        System.out.println("Output: "+product);
    }
}
Output
Enter first number: 22.5
Enter second number: 44.8
Output: 1007.9999999999999


0 Comments