w3resource

C Exercises: Accept a grade and display equivalent description


20. Grade to Description Mapping

Write a program in C to accept a grade and display the equivalent description:

Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail

Visual Presentation:

Accept a grade and display equivalent description


Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.
#include <ctype.h>  // Include the header file for character handling functions.
#include <string.h>  // Include the header file for string handling functions.

void main()
{
    char notes[15];  // Declare a character array to store notes.
    char grd;  // Declare a character variable to store the grade.

    printf("Input the grade :");  // Prompt user for input of grade.
    scanf("%c", &grd);  // Read and store the grade.

    grd = toupper(grd);  // Convert the grade to uppercase using 'toupper' function.

    switch(grd)  // Start a switch statement based on the grade.
    {
        case 'E':
            strcpy(notes, " Excellent");  // Copy corresponding note for grade 'E'.
            break;
        case 'V':
            strcpy(notes, " Very Good");  // Copy corresponding note for grade 'V'.
            break;
        case 'G':
            strcpy(notes, " Good ");  // Copy corresponding note for grade 'G'.
            break;
        case 'A':
            strcpy(notes, " Average");  // Copy corresponding note for grade 'A'.
            break;
        case 'F':
            strcpy(notes, " Fails");  // Copy corresponding note for grade 'F'.
            break;
        default :
            strcpy(notes, "Invalid Grade Found. \n");  // Copy message for invalid grade.
            break;
    }
    printf("You have chosen  : %s\n", notes);  // Print the chosen note.
} 

Output:

Input the grade :A                                                                                            
You have chosen  :  Average  

Flowchart:

Flowchart: Accept a grade and display equivalent description


For more Practice: Solve these Related Problems:

  • Write a C program to convert an input grade to its description, handling both uppercase and lowercase letters.
  • Write a C program to accept multiple grades in one input and output their corresponding descriptions.
  • Write a C program to map a grade to its description using a switch-case construct with error checking for invalid grades.
  • Write a C program to convert a grade to its description using an array of strings and index mapping.

C Programming Code Editor:



Previous: Write a program in C to calculate and print the Electricity bill of a given customer.
Next: Write a program in C to read any day number in integer and display day name in the word.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.