某商品出售按购买数量打折,如果购买数量超过30个则按95折销售,输入商品数量和商品单价(元),输出所购商品的总价的C语言程序
时间: 2023-08-31 17:39:48 浏览: 64
### 回答1:
```c
#include <stdio.h>
int main() {
int quantity;
float price, total;
printf("请输入商品数量和单价:");
scanf("%d %f", &quantity, &price);
if (quantity > 30) {
total = quantity * price * 0.95; // 满30个九五折
} else {
total = quantity * price;
}
printf("商品的总价为:%.2f元\n", total);
return 0;
}
```
### 回答2:
```c
#include <stdio.h>
int main() {
int quantity; // 商品数量
float price; // 商品单价
float total; // 总价
printf("请输入商品数量:");
scanf("%d", &quantity); // 输入商品数量
printf("请输入商品单价(元):");
scanf("%f", &price); // 输入商品单价
if (quantity > 30) { // 如果购买数量超过30个
total = quantity * price * 0.95; // 按95折计算总价
} else {
total = quantity * price; // 否则按原价计算总价
}
printf("所购商品的总价为:%.2f元\n", total);
return 0;
}
```
运行程序后,首先会要求输入商品数量,然后要求输入商品单价。程序会根据输入的商品数量和单价进行计算,并输出所购商品的总价。如果购买数量超过30个,则会按95折的价格计算总价。最后程序会输出所购商品的总价。
### 回答3:
#include <stdio.h>
int main() {
int quantity; // 商品数量
float unitPrice; // 商品单价
float totalPrice = 0; // 总价
printf("请输入商品数量:");
scanf("%d", &quantity);
printf("请输入商品单价(元):");
scanf("%f", &unitPrice);
if (quantity > 30) { // 如果购买数量超过30个
totalPrice = quantity * unitPrice * 0.95; // 95折销售
} else {
totalPrice = quantity * unitPrice;
}
printf("所购商品的总价为:%.2f元", totalPrice);
return 0;
}
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)