This program determines a student’s grade based on marks obtained, using an if-else ladder.
C Program
#include <stdio.h>
int main() {
float marks;
printf("Enter marks obtained (out of 100): ");
scanf("%f", &marks);
if (marks < 0 || marks > 100) {
printf("Invalid marks.\n");
}
else if (marks >= 90) {
printf("Grade: A+\n");
}
else if (marks >= 80) {
printf("Grade: A\n");
}
else if (marks >= 70) {
printf("Grade: B\n");
}
else if (marks >= 60) {
printf("Grade: C\n");
}
else if (marks >= 40) {
printf("Grade: D\n");
}
else {
printf("Grade: F (Fail)\n");
}
return 0;
}
Explanation
- Each
else ifchecks a progressively lower marks threshold — the first true condition determines the grade. - Because the checks go from highest to lowest, a student scoring 95 correctly matches the first branch (
>= 90) and skips the rest. - Marks below 40 fall through to the final
else, which marks the student as failed. - An initial validity check rejects out-of-range input.
Sample Output
Enter marks obtained (out of 100): 76
Grade: B