Posted on 2007-06-28 21:44
停留的风 阅读(348)
评论(0) 编辑 收藏 所属分类:
C语言学习历程
求几门课程的平均成绩以及挂科的门数
#include <stdio.h>
int main(void)
{
int number,i,grade;
int gradeTotal=0;
int failureCount=0;
float average;
printf("How many grades will you be entering?");
scanf("%i",&number);
for(i=1;i<=number;i++)
{
printf("Enter grade #%i: ",i);
scanf("%i",&grade);
gradeTotal=gradeTotal+grade;
if(grade<65)
++failureCount;
}
average=(float)gradeTotal/number;
printf("\nGrade average = %.2f\n",average);//这是个格式化字符串,保留两位有效数字
printf("Number of failures =%i\n",failureCount);
return 0;
}