用c语言解决下列问题:求输入两个数的和、差、积、商和余数(如果是浮点数,则需将其强转为整型后计算余数)。请改正程序中的错误,使它能得出正确的结果。 #include <stdio.h> main() { float a, b; float sum, minus, product, quotient; int remainder; printf("\n请输入两个数:\n"); scanf("%f\n%f", a, &b); sum = a + b; minus = a - b; product = a * b; quotient = a / b; remainder = a % b; printf("和为:%.2f\n", sum); printf("差为:%.2f\n", minus); printf("积为:%.2f\n", product); printf("商为:%.2f\n", quotient); printf("余数为:%d\n", remainder); }
时间: 2024-01-12 16:05:18 浏览: 181
c语言求余数的实例讲解
5星 · 资源好评率100%
```c
#include <stdio.h>
int main()
{
float a, b;
float sum, minus, product, quotient;
int remainder;
printf("\n请输入两个数:\n");
scanf("%f%f", &a, &b); // 修正了 scanf 函数的调用方式,去掉了 \n
sum = a + b;
minus = a - b;
product = a * b;
quotient = a / b;
remainder = (int)a % (int)b; // 对 a 和 b 强制转换为整型后再计算余数
printf("和为:%.2f\n", sum);
printf("差为:%.2f\n", minus);
printf("积为:%.2f\n", product);
printf("商为:%.2f\n", quotient);
printf("余数为:%d\n", remainder);
return 0;
}
```
改正了 `scanf` 函数的调用方式,去掉了字符串中的 `\n`,并将浮点数转换为整型后再计算余数。
阅读全文