Java Program to Multiply Two Numbers

You are currently viewing Java Program to Multiply Two Numbers

Here, You are going to learn the topic about Java Program to Multiply Two Numbers. Below, We can see the two example programs with two different types of inputs like the “positive number” and the “floating point number”. Okay, Let’s start the article.

Java Program to Multiply Two Numbers

Now, We can give the positive integer input to multiply two numbers in java.

import java.util.Scanner;

public 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);
    }
}

Execution

Initially, We need to import the java.util.scanner class into this program. Secondly, We need to assign the respective variables which will use to store both input and output values.

The program asks you to enter the input values after running this program. The output will be multiplied and the new output will be stored in the new variable using the new scanner() class.

Finally, We will see the result on the screen using the system.out.printIn() function.

Output

The output of the java program to multiply two numbers will be provided below for your reference.

Enter first number: 2
Enter second number: 3
Output: 6
Read Also : Java Program to Divide Two Numbers

Note

We can also Multiply Two Numbers in C and Multiply Two Numbers in C++. If you want to learn then please refer those article which we have already posted on our website.

Java Program to Multiply Two Numbers

We can see the example program to multiply two numbers using java with the floating-point number input.

import java.util.Scanner;

public 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);
    }
}

Explanation

Here, We can provide the floating-point number inputs instead of the positive integer inputs. Otherwise, Both programs’ executions are the same.

Output

The input values will be either your wish or a user’s choice. This is the output of the java program to multiply two numbers.

Enter first number: 1.5
Enter second number: 2.5
Output: 3.75

Conclusion

I hope this article helps you to Multiply Two Numbers using Java. If you face any issues please let me know via the comment section. Share this article with other Java developers via social networks. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply