【C语言】C语言用数组算平均数,并输出大于平均数的数

题目
让用户输入一系列的正整数,最后输入“-1”表示输入结束,然后程序计算出这些数的平均数,最后输出输入数字的个数和平均数以及大于平均数的数

代码

#include
int main()
{
	int x;
	double sum = 0;
	int cnt = 0;
	int number[100];
	scanf("%d", &x);
	while(x!=-1){
		number[cnt] = x;
		sum = sum + x;
		cnt++;
		scanf("%d", &x);
	}
	if(cnt>0){
		printf("%f\n",sum/cnt);
		int i;
		for(i=0; i<cnt; i++){
			if(number[i] > sum/cnt){
				printf("%d\n", number[i]);
			}
		}
	}
	
	return 0;
}

运行结果

【C语言】C语言用数组算平均数,并输出大于平均数的数_第1张图片
注:该程序仅适用于输入的数不大于100个,当输入的数大于100个后会出错!!!

你可能感兴趣的:(C语言程序,c语言,开发语言)