Java Program to Multiply Two Floating-Point Numbers

JavaObject Oriented ProgrammingProgramming

In this article, we will understand how to multiply two floating-point numbers. Floating point numbers are numbers with decimal values. Float data type is a single-precision 32-bit IEEE 754 floating point. It is mainly used to save memory in large arrays of floating-point numbers. The Default value is 0.0f. Float data type is never used for precise values such as currency.

Below is a demonstration of the same −

Input

Suppose our input is −

Value_1: 12.4f
Value_2: 15.7f

Output

The desired output would be −

Result : 194.68f

Algorithm

Step 1- Start
Step 2- Declare three floating points: input_1, input_2 and product
Step 3- Prompt the user to enter two floating point value/ define the floating-point values
Step 4- Read the values
Step 5- Multiply the two values using a multiplication operator (*)
Step 6- Display the result
Step 7- Stop

Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool run button.

import java.util.Scanner;
public class MultiplyFloatValues{
   public static void main(String[] args){
      float input_1, input_2, my_prod;
      Scanner my_scan = new Scanner(System.in);
      System.out.println("A reader object has been defined ");
      System.out.println("Enter the first floating point number: ");
      input_1 = my_scan.nextFloat();
      System.out.println("Enter the second floating point number: ");
      input_2 = my_scan.nextFloat();
      my_prod = input_1 * input_2;
      System.out.println("\nThe product of the two values is: " + my_prod);
   }
}

Output

A reader object has been defined
Enter the first floating point number: 12.4
Enter the second floating point number: 15.7
The product of the two values is: 194.68

Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console

public class MultiplyFloatValues{
   public static void main(String[] args){
      float value_1, value_2, my_prod;
      value_1 = 12.4f;
      value_2 = 15.7f;
      System.out.printf("The two numbers are %.2f and %.2f ",value_1, value_2 );
      my_prod = value_1 * value_2;
      System.out.println("\nThe product of the two values are: " + my_prod);
   }
}

Output

The two numbers are 12.40 and 15.70
The product of the two values are: 194.68
raja
Updated on 21-Feb-2022 09:24:26

Advertisements