C Program To Find Grace Marks of Student Using Switch Case

Write a C program to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in. Use the following logic:

– If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. Otherwise the grace is of 5 marks per subject.

– If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. Otherwise the grace is of 4 marks per subject.

– If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. Otherwise the grace is of 5 marks per subject.

Related Read:
if else statement in C
Switch Case Default In C Programming Language

Video Tutorial: C Program To Find Grace Marks of Student Using Switch Case



YouTube Link: https://www.youtube.com/watch?v=wpfh-tLWbEo [Watch the Video In Full Screen.]

Source Code: C Program To Find Grace Marks of Student Using Switch Case

  1. #include<stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int grade, grace = 0, failed;  
  6.   
  7.     printf("Enter the class obtained by the student\n");  
  8.     scanf("%d", &grade);  
  9.   
  10.     printf("How many subjects has the student failed\n");  
  11.     scanf("%d", &failed);  
  12.   
  13.     switch(grade)  
  14.     {  
  15.         case 1:  
  16.                 if(failed > 3)  
  17.                     grace = 0;  
  18.                 else  
  19.                     grace = 5;  
  20.   
  21.                 break;  
  22.   
  23.         case 2:  
  24.                 if(failed > 2)  
  25.                     grace = 0;  
  26.                 else  
  27.                     grace = 4;  
  28.   
  29.                 break;  
  30.   
  31.         case 3:  
  32.                 if(failed > 1)  
  33.                     grace = 0;  
  34.                 else  
  35.                     grace = 5;  
  36.   
  37.                 break;  
  38.   
  39.          default: printf("You entered wrong class for the student\n");  
  40.   
  41.     }  
  42.   
  43.     if(grade == 1 || grade == 2 || grade == 3)  
  44.     {  
  45.         printf("The student has obtained a grace marks of %d per subject\n",   
  46.               grace);  
  47.     }  
  48.   
  49.     return 0;  
  50. }  

Output 1
Enter the class obtained by the student
1
How many subjects has the student failed
2
The student has obtained a grace marks of 5 per subject

Output 2
Enter the class obtained by the student
2
How many subjects has the student failed
3
The student has obtained a grace marks of 0 per subject

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert