用C语言试用if else if else if ...else实现求4个数中最大者的程序,
时间: 2023-11-27 20:04:28 浏览: 29
以下是用C语言实现求4个数中最大者的程序:
```c
#include <stdio.h>
void main()
{
int a = 1, b = 2, c = 3, d = 4;
int max = a;
if (b > max) {
max = b;
}
if (c > max) {
max = c;
}
if (d > max) {
max = d;
}
printf("最大值为:%d", max);
}
```
相关问题
用c语言找两个数中的最大者
可以使用条件语句来找到两个数中的最大值,例如:
```c
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max;
if (a > b) {
max = a;
} else {
max = b;
}
printf("The maximum number is %d\n", max);
return 0;
}
```
在上面的代码中,我们定义了两个变量 `a` 和 `b`,然后使用条件语句 `if-else` 来比较它们的大小,并将较大的值赋给变量 `max`。最后,我们输出 `max` 的值,即为两个数中的最大值。
c语言求3个字符串(每串不超过20个字符)中的最大者。补充完善程序,以实现其功能。
以下是一个示例程序,可以求出3个字符串中的最大者:
```
#include <stdio.h>
#include <string.h>
int main() {
char str1[20], str2[20], str3[20];
printf("请输入三个字符串,每个字符串不超过20个字符:\n");
scanf("%s%s%s", str1, str2, str3);
int len1 = strlen(str1);
int len2 = strlen(str2);
int len3 = strlen(str3);
if (len1 >= len2 && len1 >= len3) {
printf("最大的字符串是:%s\n", str1);
} else if (len2 >= len1 && len2 >= len3) {
printf("最大的字符串是:%s\n", str2);
} else {
printf("最大的字符串是:%s\n", str3);
}
return 0;
}
```
该程序首先定义了三个字符串 `str1`、`str2` 和 `str3`,分别用于存储输入的三个字符串。然后通过 `scanf` 函数从标准输入中读入三个字符串。接下来使用 `strlen` 函数分别求出三个字符串的长度,并通过比较得到最长的字符串。最后将最长的字符串输出即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)