Skip to content

Latest commit

 

History

History
64 lines (44 loc) · 1.44 KB

File metadata and controls

64 lines (44 loc) · 1.44 KB

C 程序:将两个浮点数相乘

原文: https://www.programiz.com/c-programming/examples/product-numbers

在此示例中,计算用户输入的两个浮点数的乘积并将其打印在屏幕上。

要理解此示例,您应该了解以下 C 编程主题:


将两个数字相乘的程序

#include <stdio.h>
int main() {
    double a, b, product;
    printf("Enter two numbers: ");
    scanf("%lf %lf", &a, &b);  

    // Calculating product
    product = a * b;

    // Result up to 2 decimal point is displayed using %.2lf
    printf("Product = %.2lf", product);

    return 0;
} 

输出

Enter two numbers: 2.4
1.12
Product = 2.69 

在该程序中,要求用户输入两个数字,分别存储在变量ab中。

printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b); 

然后,求值ab的乘积,并将结果存储在product中。

product = a * b; 

最后,使用printf()在屏幕上显示product

printf("Product = %.2lf", product); 

注意,结果使用%.2lf转换字符四舍五入到小数点后第二位。