Switch with C

#include <stdio.h>

int main() {
    // Declare a variable
    char grade;

    // Prompt the user for input
    printf("Enter your grade (A, B, C, D, or F): ");

    // Read user input
    scanf(" %c", &grade); // Note the space before %c to consume any leading whitespace

    // Evaluate the grade using a switch statement
    switch (grade) {
        case 'A':
            printf("Excellent!\n");
            break;
        case 'B':
            printf("Good!\n");
            break;
        case 'C':
            printf("Satisfactory\n");
            break;
        case 'D':
            printf("Needs Improvement\n");
            break;
        case 'F':
            printf("Fail\n");
            break;
        default:
            printf("Invalid grade entered.\n");
    }

    return 0;
}
chevron_left
chevron_right

Leave a comment

Your email address will not be published. Required fields are marked *

Comment
Name
Email
Website