每瓶果汁20元,五瓶以上包括五瓶,十瓶以下,打九折。用c语言怎样编程
时间: 2023-08-31 13:31:08 浏览: 49
### 回答1:
#include <stdio.h>
int main() {
int amount; // 购买数量
float price; // 单价
float total; // 总价
printf("请输入购买数量:");
scanf("%d", &amount);
if (amount >= 5 && amount <= 10) { // 五瓶以上包括五瓶,十瓶以下,打九折
price = 20 * 0.9;
} else {
price = 20;
}
total = amount * price;
printf("总价为:%.2f元\n", total); // 保留两位小数输出
return 0;
}
### 回答2:
要用C语言编程实现每瓶果汁20元,购买五瓶以上必须打九折,购买十瓶以下也必须打九折,我们可以先定义一个变量n表示购买的瓶数,再定义一个变量price表示每瓶的价格,最后计算总价。
首先,我们可以通过用户输入来获取购买的瓶数n。
然后,我们可以使用if语句来根据购买的瓶数n判断是否满足打折条件。如果n大于等于5且小于等于10,则将total_price设置为n乘以原价20乘以0.9;否则,total_price设置为n乘以原价20。
最后,输出总价即可。
下面是一个示例代码:
```c
#include <stdio.h>
int main() {
int n; // 购买的瓶数
int price = 20; // 每瓶果汁的价格
float total_price; // 总价
printf("请输入购买的瓶数:");
scanf("%d",&n);
if(n >= 5 && n <= 10) {
total_price = n * price * 0.9;
}
else {
total_price = n * price;
}
printf("总价为:%.2f元\n", total_price);
return 0;
}
```
在运行程序时,用户需输入一个整数表示购买的瓶数。程序将根据购买的瓶数输出相应的总价。例如,如果用户输入7,则输出总价为126.00元。
### 回答3:
在C语言中,可以使用以下代码来计算购买果汁的价格:
```c
#include <stdio.h>
int main() {
int num_of_bottles;
float juice_price = 20.0;
float total_price;
printf("请输入购买瓶数:");
scanf("%d", &num_of_bottles);
if(num_of_bottles >= 5 && num_of_bottles <= 10) {
total_price = num_of_bottles * juice_price * 0.9;
} else {
total_price = num_of_bottles * juice_price;
}
printf("总价为:%f元\n", total_price);
return 0;
}
```
在程序中,我们首先声明了变量`num_of_bottles`用来存储购买的瓶数,`juice_price`用来保存每瓶果汁的价格,`total_price`用来存储总价格。然后使用`printf`函数来提示用户输入购买的瓶数,使用`scanf`函数来接受输入的值。
接着,我们使用条件语句来判断购买瓶数是否在5到10之间,如果是,则使用`num_of_bottles * juice_price * 0.9`来计算总价格,这里的0.9表示打九折;如果不是,则使用`num_of_bottles * juice_price`计算总价格。
最后,使用`printf`函数来输出总价格。
运行程序后,用户将会被提示输入购买的瓶数,程序会根据用户的输入进行计算,并输出总价格。