Write a Java Program to Multiply two Floating Point Numbers

Multiplying two floating-point numbers is a fundamental operation in programming, especially when dealing with financial calculations or scientific computations.

In this tutorial, we will explore how to multiply two floating-point numbers using Java.


Java is a popular programming language that is widely used for developing various applications.

It supports a wide range of data types, including floating-point numbers, which are used to represent decimal values.

In Java, we can use the float or double data types to store floating-point numbers.

To multiply two floating-point numbers in Java, we can use the multiplication operator (*).

The following code demonstrates how to multiply two floating-point numbers in Java:

public class Main {
    public static void main(String[] args) {
        float num1 = 1.5f;
        float num2 = 2.5f;
        float result = num1 * num2;
        System.out.println("Result: " + result);
    }
}

In the above code, we have declared two floating-point variables num1 and num2, and assigned them the values 1.5 and 2.5, respectively.

We then multiply these two variables using the multiplication operator (*) and store the result in another variable result.

Finally, we print the result to the console using the System.out.println() method.

It is important to note that when working with floating-point numbers, we may encounter rounding errors due to the way these numbers are represented in memory.

To minimize the impact of rounding errors, it is recommended to use the double data type instead of float, as double has a higher precision and can represent a wider range of values.

Here’s an example of how to multiply two double-precision floating-point numbers in Java:

public class Main {
    public static void main(String[] args) {
        double num1 = 1.5;
        double num2 = 2.5;
        double result = num1 * num2;
        System.out.println("Result: " + result);
    }
}

In the above code, we have declared two double-precision floating-point variables num1 and num2, and assigned them the values 1.5 and 2.5, respectively.

We then multiply these two variables using the multiplication operator (*) and store the result in another variable result.

Finally, we print the result to the console using the System.out.println() method.


In conclusion, multiplying two floating-point numbers in Java is a straightforward operation that can be accomplished using the multiplication operator (*).

When working with floating-point numbers, it is important to choose the appropriate data type based on the required precision and range of values.