assign a letter grade based on the test percentage according to the following rules
80 to 100 A
70 to 79 B
60 to 69 C
50 to 59 D
0-49 F
display an appropriate message with the test percentage and letter grade for the student . Accumulate the number and total of all the percentages . after all the input calculate the average percentage from valid percentages entered .display the average percentage, number of passes, number of fails and number of invalid percentages. using for, while and do...while program.
#include<stdio.h>
#include<stdlib.h>
char getGrade(int percentage);
//The start point of the program
int main(){
int percentages[100];
int tp=0;
int i=0;
int n=0;
float averagePercentage;
float sum=0;
int numberPasses=0;
int numberFails=0;
int invalidPercentages=0;
while(tp!=-99){
printf("Enter test percentage [-99 to exit]: ");
scanf("%d",&tp);
if(tp!=-99 && tp>=0 && tp<=100){
//Accumulate the number and total of all the percentages
percentages[n++]=tp;
}else{
invalidPercentages++;
}
}
printf("\nTest percentage\t\tLetter grade\n");
for(i=0;i<n;i++){
printf("%d\t\t\t%c\n",percentages[i],getGrade(percentages[i]));
}
i=0;
do{
sum+=percentages[i];
if(percentages[i]>=50){
numberPasses++;
}else{
numberFails++;
}
i++;
}while(i<n);
//calculate the average percentage from valid percentages entered
averagePercentage=sum/n;
//display the average percentage, number of passes,
//number of fails and number of invalid percentages
printf("\nThe average percentage: %.2f\n",averagePercentage);
printf("The number of passes percentages: %d\n",numberPasses);
printf("The number of fails percentages: %d\n",numberFails);
printf("The number of invalid percentages: %d\n",invalidPercentages);
getchar();
getchar();
return 0;
}
char getGrade(int percentage){
//80 to 100 A
if(percentage>=80 && percentage<=100){
return 'A';
}
//70 to 79 B
if(percentage>=70 && percentage<=79){
return 'B';
}
//60 to 69 C
if(percentage>=60 && percentage<=69){
return 'C';
}
//50 to 59 D
if(percentage>=50 && percentage<=59){
return 'D';
}
//0-49 F
return 'F';
}
Comments
Leave a comment