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:
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 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 <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:
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:
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
- Looping statement.
- Iterative statement.
- Branching statement.
- None of the above.
2] Which of the following are decision making and branching statements.
- if else statement.
- if statement.
- switch statement.
- Above all of them.
3] case values are
- unique.
- duplicate.
- Can not say.
- Depends on program.
4] default statement in switch
- Is optional.
- It is not an optional.
- If present also it is not executed.
- 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.