letter-grade.c: Change if-else chain to switch statement

This commit is contained in:
BBaoVanC 2020-10-20 20:54:24 -05:00
parent 2beb50bb25
commit b45e3670cc
No known key found for this signature in database
GPG Key ID: 6D74C8B0E7D791C2

View File

@ -7,17 +7,27 @@ int main() {
if ((input >= 0) && (input <= 100)) {
char letter;
if ((input/10) >= 9) {
letter = 'A';
} else if ((input/10) >= 8) {
letter = 'B';
} else if ((input/10) >= 7) {
letter = 'C';
} else if ((input/10) >= 6) {
letter = 'D';
} else {
letter = 'F';
int inputRange = input/10;
switch(inputRange) {
case 10:
case 9:
letter = 'A';
break;
case 8:
letter = 'B';
break;
case 7:
letter = 'C';
break;
case 6:
letter = 'D';
break;
case 5:
default:
letter = 'F';
}
printf("Your letter grade is %c\n", letter);
} else {
printf("ERROR: Input must be no more than 100 and no less than 0\n");