Pages

Thursday, October 29, 2020

Java Program to Multiply Two Floating Point Numbers

1. Overview

In this article, you'll learn how to get the multiplication of two floating numbers in java

To understand this, you need to have the knowledge on the following areas.

Simple Hello World Example

Variables in java

This is as same as the multiplication of two numbers with * operator.

In the previous article shown how to multiply two integer numbers.

Java Program to Multiply Two Floating Point Numbers


2. Example To Multiply Two Floating Numbers

In the below program created two floating variables f1, f2 with values 2.5f and 4.0f.

Here, you might have noticed that 'f' is added at the end of each value. This tells to the compiler that floating values are assigned to f1 and f2. If you do not keep the 'f' at the end then these will be treated as double values. By default, number literal value type is double.

package com.javaprogramto.programs.maths;

public class MultiplyFloatingNumbers {

	public static void main(String[] args) {

		// first floating number
		float f1 = 2.5f;

		// second floating number
		float f2 = 4.0f;

		// result of multiplication of two numbers.
		float result = f1 * f2;

		// printing
		System.out.println("Result is : " + result);

	}

}
 

Output:

Result is : 10.0
 

f1 and f2 are multiplied using '*' operator and produced the result is stored in the float variable.

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

3. Conclusion

In this article, you've seen how to multiply two floating numbers in java using * operator.

As usual, Shown example is on GitHub.

Dynamic Variable Initialization

Ref

No comments:

Post a Comment

Please do not add any spam links in the comments section.