Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để nhân hai số dấu phẩy động

Trong bài này, chúng ta sẽ hiểu cách nhân hai số dấu phẩy động. Số dấu phẩy động là số có giá trị thập phân. Kiểu dữ liệu float là một dấu chấm động IEEE 754 32 bit chính xác đơn. Nó chủ yếu được sử dụng để tiết kiệm bộ nhớ trong mảng lớn các số dấu phẩy động. Giá trị mặc định là 0,0f. Kiểu dữ liệu float không bao giờ được sử dụng cho các giá trị chính xác như tiền tệ.

Dưới đây là một minh chứng về điều tương tự -

Đầu vào

Giả sử đầu vào của chúng tôi là -

Value_1: 12.4f
Value_2: 15.7f

Đầu ra

Đầu ra mong muốn sẽ là -

Result : 194.68f

Thuật toán

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

Ví dụ 1

Ở đây, đầu vào đang được người dùng nhập dựa trên lời nhắc. Bạn có thể thử trực tiếp ví dụ này trong công cụ nền tảng mã hóa của chúng tôi Chương trình Java để nhân hai số dấu phẩy động .

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);
   }
}

Đầu ra

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

Ví dụ 2

Ở đây, số nguyên đã được xác định trước đó và giá trị của nó được truy cập và hiển thị trên bảng điều khiển

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);
   }
}

Đầu ra

The two numbers are 12.40 and 15.70
The product of the two values are: 194.68