(C/C++)求十个整数中的最大值(数组)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_42433334/article/details/102764548

求十个整数中的最大值

首先定义一个a[10],然后默认为a[0]存放最大值,然后遍历,分别与a[0]存放的数值进行比较大小,如果是大于a[0],则交换数值,保证max中存放较大值。
#include<stdio.h>
#include<stdlib.h>
int main()
{
	int i,max;
	int a[10];
	for (i = 0; i < 10; ++i)
	{
		scanf_s("%d\n", &a[i]);
	}
	
	max = a[0];
	for (i = 1; i < 10; ++i)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
	}
	printf("max:%d\n",max);
	system("pause");
	return 0;
}
输出结果为:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42433334/article/details/102764548