Loading your workspace…
Loading your workspace…
Read a percentage of marks from 0 to 100 and print a message for its range, using a switch statement.
#include <stdio.h>
int main(void) {
int percentage, band;
printf("Enter percentage of marks (0-100):\n");
if (scanf("%d", &percentage) != 1 || percentage < 0 || percentage > 100) {
printf("Error: percentage must be from 0 to 100\n");
return 1;
}
band = percentage / 10;
switch (band) {
case 10:
case 9:
printf("Outstanding\n");
break;
case 8:
printf("Excellent\n");
break;
case 7:
case 6:
printf("First Class\n");
break;
case 5:
printf("Second Class\n");
break;
case 4:
printf("Pass\n");
break;
default:
printf("Fail\n");
break;
}
return 0;
}
Input
75Output
Enter percentage of marks (0-100):
First ClassInput
100Output
Enter percentage of marks (0-100):
OutstandingInteger division by 10 turns each ten-mark range into a single case label, so the switch stays short. Cases 10 and 9 share one body, and 7 and 6 share another, because a missing break makes execution fall through to the next case.
Fall-through is used deliberately here. Each shared group ends with a single break so that no other band's message is printed.
Edit the program and press Run. It is free and needs no sign-in.
Copy a sample and paste it into the STDIN box in the editor before you press Run.
75
Expected output
Enter percentage of marks (0-100): First Class
100
Expected output
Enter percentage of marks (0-100): Outstanding
Integer division drops the remainder, so 75 becomes 7 and every value from 70 to 79 maps to the same case.
When a case has no break, execution continues into the next case's code.
It handles every value that no case label matches, here all bands below 4.
switch matches exact constant values, so ranges are reduced to a single value first.
Values above 100 would map to band 10 or more and print the top message incorrectly.
Back to all RCU 21BCA1C1P lab programs.