从键盘输入一个考试成绩score(知识点:scanf),将考试成绩按“及格”和“不及格”输出。
时间: 2023-05-11 08:06:50 浏览: 113
好的,根据您的要求,我可以回答这个问题。以下是代码示例:
```
#include <stdio.h>
int main() {
int score;
printf("请输入考试成绩:");
scanf("%d", &score);
if (score >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
return 0;
}
```
这段代码可以从键盘输入一个考试成绩,然后判断是否及格并输出结果。如果成绩大于等于60分,则输出“及格”,否则输出“不及格”。
相关问题
用c语言编程:输入10个学生的考试成绩,输出及格学生的平均成绩。
好的,我来解答你的问题。以下是利用 C 语言编写的代码,可以实现输入 10 个学生的考试成绩,并输出及格学生的平均成绩。
```
#include <stdio.h>
int main() {
float scores[10];
float sum = 0;
int count = 0;
// 依次输入 10 个学生的考试成绩
for (int i = 0; i < 10; i++) {
printf("请输入第 %d 个学生的成绩:", i + 1);
scanf("%f", &scores[i]);
if (scores[i] >= 60) { // 判断是否及格
sum += scores[i];
count++;
}
}
// 输出及格学生的平均成绩
if (count == 0) {
printf("没有及格的学生!");
} else {
printf("及格学生的平均成绩为:%.2f", sum / count);
}
return 0;
}
```
以上代码中,我们定义了一个长度为 10 的 float 数组 `scores`,用于存放 10 名学生的考试成绩。然后我们通过一个 for 循环,依次读取每一位学生的成绩,并进行判断。如果成绩大于等于 60 分,则累加到 `sum` 变量中,同时将 `count` 变量加 1,用于记录及格学生的人数。
最后,通过判断 `count` 变量是否为 0,若为 0 则说明没有及格的学生,否则输出及格学生的平均成绩,即 `sum / count`。
希望我的回答能够帮到你!
从键盘输入学生成绩score,按五级制输出优、良、中、及格、不及格(scanf、分支语句、printf)。
示例代码如下:
```
#include <stdio.h>
int main() {
int score;
printf("请输入学生成绩:");
scanf("%d", &score);
if (score >= 90) {
printf("优\n");
} else if (score >= 80) {
printf("良\n");
} else if (score >= 70) {
printf("中\n");
} else if (score >= 60) {
printf("及格\n");
} else {
printf("不及格\n");
}
return 0;
}
```
解释一下代码:
1. 定义一个变量`score`,用于存储输入的成绩。
2. 使用`printf`函数输出提示信息,让用户输入成绩。
3. 使用`scanf`函数读取用户输入的成绩,并存储到变量`score`中。
4. 使用分支语句(if-else)判断成绩属于哪个等级,并使用`printf`函数输出对应的等级。
5. 最后,使用`return`语句结束程序。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)