编写一个程序,输入学生人数和每个人的成绩,计算平均成绩。
注意:当输入的学生人数小于等于0时,输出平均成绩为0分!
以下是测试样例:
输入:3 90 70 80
输出:the number of students:the scores:average=80.00
输入:-1
输出:the number of students:the scores:average=0.00
输入:4 78.5 26 73.6 90.1
输出:the number of students:the scores:average=67.05
程序如下:
#include<stdio.h>
int main()
{
int a,i;
double b,c=0;
scanf("%d",&a);
if(a<=0)
{
printf("the number of students:the scores:average=0.00\n");
}
else
{
for(i=1;i<=a;i++)
{scanf(" %lf",&b); /*%lf前有一个空格*/
c=c+b;}
printf("the number of students:the scores:average=%.2f\n",c/a);
}
return 0;
}