两个浮点数相乘的 C 程序
要理解此示例,您应该具备以下C 语言编程主题的知识:
两个数相乘的程序
#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
// %.2lf displays number up to 2 decimal point
printf("Product = %.2lf", product);
return 0;
}
输出
Enter two numbers: 2.4
1.12
Product = 2.69
在这个程序中,用户被要求输入两个分别存储在变量 a
和 b
中的数字。
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
那么,product = a * b;
被评估并将结果存储在 product
.
product = a * b;
最后, 使用 printf()
函数将 product
显示在屏幕上。
printf("Product = %.2lf", product);
请注意,使用 %.2lf
转换字符将结果四舍五入到小数点后第二位。