三个数比大小的c语言程序
时间: 2024-06-13 21:05:38 浏览: 11
以下是三个数比大小的C语言程序,其中包括了三种方法:
```c
#include <stdio.h>
// 方法一:if-else法
int max(int a, int b, int c){
int max = a;
if(b > max){
max = b;
}
if(c > max){
max = c;
}
return max;
}
// 方法二:函数法
int compare(int a, int b){
return a > b ? a : b;
}
int max2(int a, int b, int c){
return compare(compare(a, b), c);
}
// 方法三:三目运算符法
int max3(int a, int b, int c){
return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
int main(){
int a = 10;
int b = 20;
int c = 30;
printf("方法一:if-else法,max = %d\n", max(a, b, c));
printf("方法二:函数法,max = %d\n", max2(a, b, c));
printf("方法三:三目运算符法,max = %d\n", max3(a, b, c));
return 0;
}
```
相关问题
比较三个数的大小c语言程序
下面是一个比较三个数大小的C语言程序:
```c
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("%d 最大\n", a);
} else if (b > a && b > c) {
printf("%d 最大\n", b);
} else {
printf("%d 最大\n", c);
}
return 0;
}
```
设计一个比较三个数大小的C语言程序
可以使用if语句来比较三个数的大小,以下是一个示例程序:
#include <stdio.h>
int main() {
int a, b, c;
printf("请输入三个整数:\n");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("%d 最大\n", a);
} else if (b > a && b > c) {
printf("%d 最大\n", b);
} else {
printf("%d 最大\n", c);
}
return ;
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)