用条件运算符编写程序 实现输入三个整数输出最大值
时间: 2023-04-02 17:00:44 浏览: 194
请看以下代码:
#include <stdio.h>
int main() {
int a, b, c, max;
printf("请输入三个整数:\n");
scanf("%d%d%d", &a, &b, &c);
max = a > b ? a : b;
max = max > c ? max : c;
printf("最大值为:%d\n", max);
return ;
}
以上程序可以实现输入三个整数,然后输出最大值。
相关问题
用c语言编写程序从输入的三个数中找出最大值和最小值
好的,以下是用C语言编写的程序,可以从输入的三个数中找出最大值和最小值:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个数,用空格隔开:");
scanf("%d %d %d", &a, &b, &c);
int max = a, min = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (b < min) {
min = b;
}
if (c < min) {
min = c;
}
printf("最大值为:%d\n", max);
printf("最小值为:%d\n", min);
return 0;
}
```
程序中使用了 `scanf` 函数来从用户输入中读取三个整数,然后使用 `if` 语句和比较运算符来比较它们之间的大小,找出最大值和最小值,并输出结果。
用c语言编写程序,有3个整数a、b、c,由键盘输入,用条件表达式求出其中的最大值和最小值并输出。
以下是用 C 语言编写的程序,可以求出三个整数 a、b、c 中的最大值和最小值:
```c
#include <stdio.h>
int main() {
int a, b, c, max, min;
printf("请输入三个整数:");
scanf("%d%d%d", &a, &b, &c);
max = a > b ? (a > c ? a : c) : (b > c ? b : c);
min = a < b ? (a < c ? a : c) : (b < c ? b : c);
printf("最大值:%d\n", max);
printf("最小值:%d\n", min);
return 0;
}
```
这个程序中,我们使用了条件表达式(也叫三目运算符)来求出最大值和最小值。如果 a 大于 b,那么我们就比较 a 和 c,否则就比较 b 和 c。最后,我们将求得的最大值和最小值输出即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)