C Language Example - Multiplying Two Floating-Point Numbers

C 语言实例C Language Examples

Input two floating-point numbers and calculate the product.

Example 1

#include <stdio.h>

int main() {
    float num1, num2, product;
   
    printf("Please enter the first floating-point number: ");
    scanf("%f", &num1);
   
    printf("Please enter the second floating-point number: ");
    scanf("%f", &num2);
   
    product = num1 * num2;
   
    printf("%.2f times %.2f equals %.2f\n", num1, num2, product);
   
    return 0;
}

Run result:

请输入第一个浮点数: 3.4
请输入第二个浮点数: 5.6
3.40 乘于 5.60 等于 19.04

Analysis:

In the above example, we first declare three variables num1, num2, and product, which are used to store the two input floating-point numbers and their product, respectively.

Then, we use the printf and scanf functions to interact with the user, receiving two floating-point numbers input by the user respectively.

Next, we assign the result of multiplying num1 and num2 to the product variable.

Finally, we use the printf function to print the calculation result.

Note,%.2fThe format specifier is used to limit the output floating-point number to two decimal places.

The following example usesscanf()Simultaneously accept two floating-point numbers input by the user:

Example 2

#include <stdio.h> int main() { double firstNumber, secondNumber, product; printf("Enter two floating-point numbers:"); // User inputs two floating-point numbers scanf("%lf %lf", &firstNumber, &secondNumber); // Multiply two floating-point numbers product = firstNumber * secondNumber; // Output the result, %.2lf keeps two decimal places printf("Result = %.2lf", product); return 0; }

Run result:

输入两个浮点数: 1.2 2.345
结果 = 2.81

C Language Examples

other extensions