求n个数的最大值的C语言代码
时间: 2023-03-08 13:17:20 浏览: 73
int max = n[0];
for (int i=1; i<n; i++)
{
if (n[i] > max)
max = n[i];
}
printf("最大值为: %d",max);
相关问题
求十个数中最大值C语言代码。
可以使用循环和条件语句来实现,具体代码如下:
#include <stdio.h>
int main() {
int nums[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int max = nums[];
for (int i = 1; i < 10; i++) {
if (nums[i] > max) {
max = nums[i];
}
}
printf("最大值为:%d\n", max);
return ;
}
c语言求n个数的最大值
在C语言中,求n个数的最大值可以使用一个变量存储最大值,然后用户一边输入一边与这个最大值比较,大就更新它。以下是一个参考代码:
```
#include <stdio.h>
#define LEN 5
int main() {
int array[LEN] = {0};
int index = 0;
int max = 0;
printf("请输入5个整数: ");
for(index = 0; index < LEN; index++) {
scanf("%d", &array[index]);
if(array[index] > max) {
max = array[index];
}
}
printf("最大值是: %d\n", max);
return 0;
}
```
另外,还可以使用另一种方法,即输入一个正整数n,再输入n个整数,输出最大值。以下是一个参考代码:
```
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, a, max, i;
scanf("%d", &n);
scanf("%d", &max);
for(i = 1; i < n; i++) {
scanf("%d", &a);
if(a > max) {
max = a;
}
}
printf("%d", max);
return 0;
}
```
相关推荐
![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)