Java Program to Multiply Two Number

Here you will get two types of Java Program i.e one program will be to read two integers and multiply them and output of the multiplication. You surely get these types of programs in the assignment while learning the Java language. By practice, this type of Java Program example can understand the language also. And the second An example is to take any two numbers that can be an integer or floating display the output of it.

Let’s start the example one:

Example 1: Program to read two integers and print the Multiplication of them:

This program will ask the user to enter two integer numbers and display the multiplication of that two number, to take user input we are using Scanner class of java, to know more about Scanner class and example of reading an integer from the system input.

import java.util.Scanner;

public class MultiplicationOfTwoNum{

    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: 5
Output: 60

Find here Eclipse Code:


Here 2nd Example: Read two integer or floating-point numbers and display the multiplication

In the above program, we can only integers. What if we want to calculate the multiplication of two float numbers? This program 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;

public class FloatingNumMultiplication{

    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: 3.5
Enter second number: 1.5
Output: 5.25




Previous
Next Post »

2 comments

Click here for comments
Anonymous
admin
18 August 2020 at 10:02 ×

Nice Content.Keep it up!

Reply
avatar
Siya
admin
20 August 2020 at 10:06 ×

Well explained example, understood.

Reply
avatar