Solved Examples · Example 27
Q.Write a C program to accept a letter for the grade obtained by the student and display the corresponding remark. The letter grade and the corresponding remarks are listed in the table (A - BEST, B - VERY GOOD, C - FAIR, D - ABSENT, E - FAIL).
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char remark[12]; /*remark is an array of 12 chars*/
char grade;
printf("Enter the grade\n");
scanf("%c", &grade);
/*Lower case letter to upper case*/
grade=to upper(grade);
switch(grade)
{
Case 'A' : strcpy (remark, "BEST");
break;
Case 'B' : strcpy (remark, "VERY GOOD");
break;
Case 'C' : strcpy (remark, "FAIR");
break;
Case 'D' : strcpy (remark, "ABSENT");
break;
Case 'F' : strcpy (remark, "FAILS");
break;
}
/*End of switch*/
Printf("RESULT; %s\n", remark);
}
/*End of main()*/
Sample output printed in the textbook:
Run1 Run2
Enter the grade Enter the grade
B G
RESULT : VERY GOOD RESULT : ERROR IN GRADE
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
char remark[12]; /*remark is an array of 12 chars*/
char grade;
printf("Enter the grade\n");
scanf("%c", &grade);
/*Lower case letter to upper case*/
grade=to upper(grade);
switch(grade)
{
Case 'A' : strcpy (remark, "BEST");
break;
Case 'B' : strcpy (remark, "VERY GOOD");
break;
Case 'C' : strcpy (remark, "FAIR");
break;
Case 'D' : strcpy (remark, "ABSENT");
break;
Case 'F' : strcpy (remark, "FAILS");
break;
}
/*End of switch*/
Printf("RESULT; %s\n", remark);
}
/*End of main()*/
Run1 Run2
Enter the grade Enter the grade
B G
RESULT : VERY GOOD RESULT : ERROR IN GRADE
Karnataka PUCTextbookLongImportance★★★★★est
81% · 124/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
A switch maps a grade letter to its remark using strcpy(); B -> VERY GOOD and an unknown grade -> ERROR IN GRADE.
This example combines three library facilities. toupper() (from ctype.h) converts a lower-case letter to upper case so 'b' and 'B' are treated alike. The switch selects the case for that letter, and strcpy() (from string.h) copies the chosen text into the character array remark, because in C a string cannot be assigned with =. The default label supplies a safe message for any unlisted grade. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.