C Exercises: Accept a grade and display equivalent description
C Conditional Statement: Exercise-20 with Solution
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:
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.
}
Sample Output:
Input the grade :A You have chosen : Average
Flowchart:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/c-programming-exercises/conditional-statement/c-conditional-statement-exercises-20.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics