Code a program to get an unknown number of test percentages from the user. Accept percentages until the user enter a percentage of -99. This number ( -99) indicates the end of the data and must not be used as part of the calculations.
Assign a letter grade based on the test percentage according to the following rules:
TEST PERCENTAGE GRADE
From 80 to 100 ‘A’
From 70 to 79 ‘B’
From 60 to 69 ‘C’
From 50 to 59 ‘D’
0 - 49 'F'
Display the test percentage and letter grade for the student.
Count how many pass percentages (50 - 100) and fail (0 - 49) percentages, as well as invalid percentages (below zero and above 100), are entered.
then calculate the average percentage (from the 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;
while(1) {
printf("Enter a percentage - ");
scanf("%d", &persantage);
if ( persantage == -99) {
break;
}
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);
}
}
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