两个数相乘的程序
#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);
然后,计算
a 和
b 的乘积,并将结果存储在
product 中。
product = a * b;
最后,
product 使用
printf()
显示在屏幕上。
printf("Product = %.2lf", product);
注意,结果使用
%.2lf
转换字符四舍五入到小数点后第二位。