用c语言写循环输入 3个商品的 单价、数量 ,并根据商品数量给与折扣,1件全价,2件9.5折,3-5件,9折,5件以上8.5折,计算所有商品的总额,并输出结果
时间: 2024-05-03 22:22:03 浏览: 22
好的,以下是用C语言写循环输入3个商品的单价、数量,并根据商品数量给与折扣,计算商品总额并输出结果的代码:
```
#include <stdio.h>
int main() {
float price, total = 0;
int quantity, count = 0;
// 循环输入3个商品的单价、数量
while (count < 3) {
printf("请输入第%d个商品的单价和数量:", count+1);
scanf("%f%d", &price, &quantity);
// 判断商品数量给与的折扣
if (quantity == 1)
total += price;
else if (quantity == 2)
total += price * 0.95;
else if (quantity >= 3 && quantity <= 5)
total += price * 0.9 * quantity;
else
total += price * 0.85 * quantity;
count++;
}
// 输出商品总额
printf("所有商品的总额为%.2f元\n", total);
return 0;
}
```
希望这可以回答您的问题。
相关问题
超市计费系统2.0版 循环输入 3个商品的 单价、数量 ,并根据商品数量给与折扣,1件全价,2件9.5折,3-5件,9折,5件以上8.5折,计算所有商品的总额,并输出结果 使用C语言编程
非常好的问题!根据您的要求,我将为您提供相应的代码:
```c
#include <stdio.h>
int main() {
float price[3];
int quantity[3];
float total = 0;
for (int i = 0; i < 3; i++) {
printf("请输入第%d个商品的单价:", i + 1);
scanf("%f", &price[i]);
printf("请输入第%d个商品的数量:", i + 1);
scanf("%d", &quantity[i]);
}
for (int i = 0; i < 3; i++) {
if (quantity[i] >= 5) {
total += price[i] * quantity[i] * 0.85;
} else if (quantity[i] >= 3) {
total += price[i] * quantity[i] * 0.9;
} else if (quantity[i] == 2) {
total += price[i] * quantity[i] * 0.95;
} else {
total += price[i] * quantity[i];
}
}
printf("所有商品的总额为:%.2f元\n", total);
return 0;
}
```
希望这个代码能够对您有所帮助!如果您还有其他问题需要解答,请随时问我哦~
用c语言从键盘输入某商品的单价和数量,求出商品的总价并输出
好的,这是一个计算问题,可以回答。请看以下代码:
```c
#include <stdio.h>
int main() {
float price, quantity, total;
printf("请输入商品单价和数量:");
scanf("%f %f", &price, &quantity);
total = price * quantity;
printf("商品的总价是:%.2f\n", total);
return 0;
}
```
输入商品单价和数量后,程序会计算出商品的总价并输出。注意,%.2f 表示保留小数点后两位。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)