A Learning Platform

SUCCESS

Success = Discipline + Consistency + Sacrifice.
Discipline - in what you do.
Consistency - in what you do.
Sacrifice - for what you do.

                   -- Manjunatha C. P. --

Grade/Result of student(switch statement) C program

    In this program we will find out the Grade or Result of students based on their percentage. switch statement is used to solve this problem.

switch statement is a decision making and branching statement in c programming language.

Grading system:

75% to 100%: Grade A – First class with distinction.
60% to 74.99%: Grade B – First class.
50% to 59.99%: Grade C – Second class.
35% to 49.99%: Grade D – Pass class.
0% to 34.99%: Grade F – Fail.

Based on your requirement you can change the percentage range for grading the students. Above grading system is just an example.


Algorithm to find the student grade/result based on given percentage using switch statement

STEP 1: START
STEP 2: READ the percentage of student
              READ percentage
STEP 3: Using switch statement find the grade of student
STEP 4: percentage  >= 75 and percentage <= 100
              PRINT Grade A and First class with distinction
  STEP 5: percentage  >= 60 and percentage < 75
              PRINT Grade B and First class
STEP 6: percentage  >= 50 and percentage < 60
              PRINT Grade C and Second class
STEP 7: percentage  >= 35 and percentage < 50
              PRINT Grade D and Pass class
STEP 8: percentage  >= 0 and percentage < 35
              PRINT Grade F and Fail
STEP 9: END


Example: C program to find the student grade/result based on given percentage using switch statement

#include <stdio.h>
#include <conio.h>
void main()
{
    int percent;
    printf("Enter your percentage: ");
    scanf("%d",&percent);
    switch (percent)
    {
    case 75 ... 100:
        printf("Grade A: First class with distinction\n");
        break;
    case 60 ... 74:
        printf("Grade B: First class\n");
        break;
    case  50 ... 59:
        printf("Grade C: Second class\n");
        break;
    case 35 ... 49:
        printf("Grade D: Pass class\n");
        break;
    case 0 ... 34:
        printf("Grade F: Fail");
        break;
    default:
        printf("Invalid percentage");
        break;
    }
    getch();
}

Output:

Enter your percentage: 87
Grade A: First class with distinction
Enter your percentage: 74.8
Grade B: First class
Enter your percentage: 59.4
Grade C: Second class
Enter your percentage: 49.7
Grade D: Pass class
Enter your percentage: 33
Grade F: Fail
Enter your percentage: 111
Invalid percentage

Explanation:

defaut:
printf(“Invalid percentage”);
break;

Above default statement is executed only when student inputs the percentage below 0 and above 100. default statement is executed when their is no matching case value is found.


QUIZ:

1] switch statement is

  1. Looping statement.
  2. Iterative statement.
  3. Branching statement.
  4. None of the above.

2] Which of the following are decision making and branching statements.

  1. if else statement.
  2. if statement.
  3. switch statement.
  4. Above all of them.

3] case values are

  1. unique.
  2. duplicate.
  3. Can not say.
  4. Depends on program.

4] default statement in switch

  1. Is optional.
  2. It is not an optional.
  3. If present also it is not executed.
  4. None of the above.

Note:

break statements are used to come out(exit) of the switch. We can combine more than one case statements with single break statement.

switch statement case values are constants or constant expressions which are called as case labels. case values are unique.

The default case is an optional case. If it is present it will be executed when there is no matching case value.