C Program to Find Grade of a Student using Function

Write a C Program to Find Grade of a Student using Function. To solve this problem we can use switch case or if-else statements. The below table shows the grading system.

Score in subjectGrade
>=90A
80-89B
70-79C
60-69D
50-59E
<50F

Prerequisites for finding grade of a student using switch case statements:-

A function is a block of code that performs a specific task. For example, the main is function and every program execution starts from the main function in C programming.

C program is made of one or more functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

Find Grade of a Student using Function and If-Else in C

#include<stdio.h>
// function to find grade using if-else
char findGrade(int score) 
{
   // check score is valid or not
   // score is valid if it belongs to 0-100
   if(score<0 || score>100) {
     return '\0';
   }

   // find grade for given score
   // for score >= 90
   if(score>=90 && score<=100)
     return 'A';

   // for score>=80 and <90
   else if(score>=80)
     return 'B';

   // for score>=70 and <80
   else if(score>=70)
     return 'C';

   // for score>=60 and <70
   else if(score>=60)
     return 'D';

   // for score>=50 and <60
   else if(score>=50)
     return 'E';

   // for score<50
   else
     return 'F';
}

// main function
int main()
{
   // variables
   int score;
   char grade;

   // take score
   printf("Enter score(0-100): ");
   scanf("%d",&score);

   // find grade
   grade = findGrade(score);

   // display grade
   if(grade=='\0')
     printf("Invalid Score");
   else
     printf("Grade: %c\n", grade);

   return 0;
}

Output:-

Enter score(0-100): 50
Grade: E

Enter score(0-100): 90
Grade: A

Enter score(0-100): 150
Invalid Score

Enter score(0-100): -30
Invalid Score

Enter score(0-100): 20
Grade: F

In the function first we checked the entered score is valid or not. If the score doesn’t belong to 0 to 100 (inclusive) then it will be treated as invalid score. In that case returns ‘\0’.

If the score is valid then we will start finding the grade based on the given score. From the table, if the score is greater than or equal to 90 then the grade will ‘A’, and so on. For a score lesser than 50 the grade will be ‘F’.

Using Function and Switch Case in C

#include<stdio.h>
// function to find grade using switch-case
char findGrade(int score) 
{
   // check score is valid or not
   // score is valid if it belongs to 0-100
   if(score<0 || score>100) {
     return '\0';
   }

   // find grade for given score
   switch( score / 10 )
   {
     case 10:
     case 9:
       return 'A';
     case 8:
       return 'B';
     case 7:
       return 'C';
     case 6:
       return 'D';
     case 5:
       return 'E';
     default:
       return 'F';
   }
}

// main function
int main()
{
   // variables
   int score;
   char grade;

   // take score
   printf("Enter score(0-100): ");
   scanf("%d",&score);

   // find grade
   grade = findGrade(score);

   // display grade
   if(grade=='\0')
     printf("Invalid Score");
   else
     printf("Grade: %c\n", grade);

   return 0;
}

The condition of the switch case is score/10, so, the score every is divided by 10 and the matched label will be executed. For score 90-100 is A, so for labels 9 and 10, the grade will be A. Similarly, for other ranges. If the score is below 50 then score/10 gives below 5 and it doesn’t match with any condition hence, the default statement will be executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply