例4-2 求平均分
例4-2 求平均分
更改例3-3,计算平均分、统计不合格人数
程序核心——if语句
程序
#include<stdio.h>
int main()
{
int count,num;
double grade,total;
num=total=count;
printf("Enter grades:");
scanf("%lf",&grade);
while(grade>=0)
{
total+=grade;
num++;
if(grade<60)
count++;
scanf("%lf",&grade);
}
if(num!=0)
{
printf("Grade average is %.2f",total/num);
printf("Number of failures is %d",count);
}
else
printf("Grade averageis 0");
}
结果
Enter grades:58 78 87 90 -1
Grade average is 78.25Number of failures is 1
--------------------------------
Process exited after 36.19 seconds with return value 0
请按任意键继续. . .
分析
重点:while语句