int main(int argc, char const *argv[])
{
int quantity, rate;
float total, dis;
printf("ENTER QUANTITY : \n");
scanf("%d", &quantity);
printf("ENTER RATE : \n");
scanf("%d", &rate);
total = quantity * rate;
dis = total - (10 / 100 * total);
if (quantity > 1000)
{
printf("%.2f", dis);
}
else
{
printf("%.2f", total);
}
return 0;
}它没有给10 %的分离,我不能understand.it的错误只是乘以..。
例如,如果我输入1001作为数量,以25作为速率,它将给出25025,而不是minnus discoount。
发布于 2022-07-23 07:34:50
10 / 100是整数除法,完全是0。然后用total乘以它,结果是0.f。
确保部门中有一个float:
dis = total - 10.f / 100 * total;或
dis = total - 10 * total / 100;
// ^^^^^^^^^^
// float或
dis = total - 0.1f * total;发布于 2022-07-23 08:15:38
只需将int rate;更改为float rate;即可调用浮点。
更改条目:
scanf( "%.2f", &rate);并且,简化输出:
total = quantity * rate;
if( quantity > 1000 )
total -= total/10.0; // Knock off 1/10th (10%). Could use: total *= 0.9;
printf("%.2f", total);https://stackoverflow.com/questions/73088801
复制相似问题