
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Multiply Given Floating Point Numbers in Java
Suppose two floating point numbers are given as operands and your task is to write a Java program to multiply the given numbers. To perform this operation, initialize two float values, multiply and store the result in another float type variable.
Float is a datatype in Java which stores numbers with fractional part.
Example Scenario:
Input 1: num1 = 1.2 Input 2: num2 = 1.4 Output: product = 1.68
Using Multiplication Operator
The multiplication operator is represented by asterisk sign (*). It is categorized under arithmetic operator in Java. It can be used to multiply float values on either side of the operator.
Example
Following is a Java program to multiply given floating point numbers.
public class MultiplyFloatingNumbers { public static void main(String args[]){ float flt1 = 12.2f; System.out.println("First floating point number:: " + flt1); float flt2 = 6.3f; System.out.println("Second floating point number:: " + flt2); // multiplying float product = flt1 * flt2; System.out.println("Product of given floating point numbers:: " + product); } }
On running this code, you will get the following result ?
First floating point number:: 12.2 Second floating point number:: 6.3 Product of given floating point numbers:: 76.86
Multiplying with BigDecimal Precision
Operation on floating point numbers are subject to rounding errors and precision loss. Therefore, Java introduces Bigdecimal class so that we can get accurate precision.
Example
In this Java program we are multiplying floating point numbers using BigDecimal for precision.
import java.math.BigDecimal; public class MultiplyFloatingNumbers { public static void main(String[] args) { float flt1 = 2.2f; System.out.println("First floating point number:: " + flt1); float flt2 = 4.3f; System.out.println("Second floating point number:: " + flt2); // Converting float to BigDecimal BigDecimal bd1 = BigDecimal.valueOf(flt1); BigDecimal bd2 = BigDecimal.valueOf(flt2); // Multiplying BigDecimal product = bd1.multiply(bd2); System.out.println("The product is: " + product); } }
Output of the above code is as follows ?
First floating point number:: 2.2 Second floating point number:: 4.3 The product is: 9.460000624656686494947038590908
Multiplying by User-Defined function
In this approach, create a user-defined function which accept two floating point numbers as arguments and using the multiplication operator, find the product of the operands.
Example
Here is the practical demonstration of the above discussed approach ?
public class Main { // method to multiply public static float multiplyFloatingNumbers(float f1, float f2) { return f1 * f2; } public static void main(String[] args) { float flt1 = 2.2f; System.out.println("First floating point number:: " + flt1); float flt2 = 4.3f; System.out.println("Second floating point number:: " + flt2); double product = multiplyFloatingNumbers(flt1, flt2); System.out.printf("The product is: %.2f", product); } }
On executing, this code will produce following result ?
First floating point number:: 2.2 Second floating point number:: 4.3 The product is: 9.46