跳到主要内容

C 编程:乘法两个浮点数的程序

要理解这个示例,你应该具备以下C语言编程相关知识:

程序计算两个数字的乘积

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

// 计算乘积
product = a * b;

// %.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 转换字符四舍五入到小数点后第二位。