Java Program to Multiply Two Numbers

In this post, we will learn how to multiply two numbers using Java Programming language.

This program asks the user to enter two numbers, then it computes the product of two numbers using arithmetic operators.

So, without further ado, let’s begin this tutorial.

Java Program to Multiply Two Numbers

// Java Program to Multiply Two Numbers
import java.util.Scanner;
public class MultiplyNumbers{
    public static void main(String[] args){
        int num1, num2, product;
        
        // Asking for input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number: ");
        num1 = sc.nextInt();
        System.out.println("Enter second number: ");
        num2 = sc.nextInt();
        
        // Multiplication of two numbers
        product = num1 * num2;
        
        // Displaying output
        System.out.println("The product of two numbers: "+ product);
    }
}

Output

Enter first number: 7
Enter second number: 6
The product of two numbers: 42

How Does This Program Work ?

        int num1, num2, product;

In this program, we have declared three int data type variables named num1, num2 and product.

        // Asking for input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first number: ");
        num1 = sc.nextInt();
        System.out.println("Enter second number: ");
        num2 = sc.nextInt();

Then, the user is asked to enter the first and second number.

        // Multiplication of two numbers
        product = num1 * num2;

We calculate the product of two numbers using multiplication (*) operator.

        // Displaying output
        System.out.println("The product of two numbers: "+ product);

Finally, the product of two numbers is displayed on the System.out.println function.

Java Program to Multiply Two Floating Point Numbers

// Java Program to Multiply Two Floating Point Numbers
import java.util.Scanner;
public class FloatingProduct{
    public static void main (String[] args){
        double a, b, result;
        
        // Asking for input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter first floating point number: ");
        a = sc.nextDouble();
        System.out.println("Enter second floating point number: ");
        b = sc.nextDouble();
        
        // Product
        result = a * b;
        
        // Displaying the result
        System.out.println("Product of two numbers: " + result);
    }
}

Output

Enter first floating point number: 
7.52
Enter second floating point number: 
4.53
Product of two numbers: 34.0656

Conclusion

I hope after going through this post, you understand how to multiply two numbers using Java Programming language.

If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to guide you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *