c语言中for循环计算多个整数的和及平均值
1、
#include <stdio.h>
int main(void)
{
int num, counts, temp;
int sum =0;
puts("please input an num.");
printf("num = "); scanf("%d", &num);
for (counts = 1; counts <= num; counts++ )
{
printf("NO.%d = ", counts);
scanf("%d", &temp);
sum += temp;
}
printf("sum = %d\n", sum);
printf("mean = %f\n", (double)sum / num);
return 0;
}
↓
#include <stdio.h> int main(void) { int num, temp, counts, sum = 0; puts("please input an integer."); printf("num = "); scanf("%d", &num); for (counts = 0; counts < num; counts++) { printf("NO.%d = ", counts + 1); scanf("%d", &temp); sum += temp; } printf("sum = %d\n", sum); printf("mean = %f\n", (double)sum / num); return 0; }