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
#include <stdio.h>
int main()
{
int persantage;
char grade;
int countPass=0, countFail=0, countInvalid=0;
int total=0;
char ans;
double avg;
do {
printf("Enter an test percentage - ");
scanf("%d", &persantage);
if (persantage > 100 || persantage < 0) {
countInvalid++;
grade = '\0';
}
else if (persantage >= 80) {
countPass++;
total += persantage;
grade = 'A';
}
else if (persantage >= 70) {
countPass++;
total += persantage;
grade = 'B';
}
else if (persantage >= 60) {
countPass++;
total += persantage;
grade = 'C';
}
else if (persantage >= 50) {
countPass++;
total += persantage;
grade = 'D';
}
else {
countFail++;
total += persantage;
grade = 'F';
}
if (grade != '\0') {
printf("The grade is %c\n", grade);
}
printf("Another grade? (y/N) - ");
fgetchar(); // to skip eol
ans = fgetchar();
} while (ans == 'y' || ans == 'Y');
if (countPass + countFail) {
avg = (double) total / (double) (countPass + countFail);
printf("Average percantage is %.2f%%\n", avg);
}
printf("%d passed, %d failed, %d invalid entries\n", countPass, countFail, countInvalid);
return 0;
}
Comments
Leave a comment