Java Program to Multiply two Floating Point Numbers

Java Program to Multiply two Floating Point Numbers

What are Float Values?

In this Article, we will write a Program to Multiply two Floating Point Numbers in Java.
In Java, a float value is a primitive data type that represents a single-precision 32-bit floating point number. It is used to store real numbers with a fractional component, and it has a range of approximately 3.4 x 10^(-38) to 3.4 x 10^(38).

Float Values:

Float values in Java can be used in mathematical operations, comparisons, and other operations just like any other numeric data type. However, because they are subject to rounding errors and precision loss, it is important to be careful when using them in critical calculations, especially when comparing two float values for equality.

Syntax:

float myFloat = 3.14f;

Java Program to Multiply two Floating Point Numbers:

Run
import java.util.*;

public class Main {
  public static void main(String[] args){
    // Initializing two float variables  
    float num1 = 3.14f;
    float num2 = 2.5f;
    
    // Multiplying two float variables
    float result = num1 * num2;
    
    // Printing the final result
    System.out.println("The product of " + num1 + " and " + num2 + " is " + result);
  }
}

Output:

The product of 3.14 and 2.5 is 7.8500004.

Explanation:

In the above program, we declared two float variables num1 and num2 and initialize them with the values 3.14f and 2.5f, respectively. We then multiply the two numbers using the * operator and store the result in a third float variable called result.

Finally, we use the System.out.println() method to print out the result to the console, along with a message that indicates what the result represents. When you run this program, the output should be:

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription