➤ Hello World! in C++
➤ Print Number in C++
➤ Add 2 Numbers C++
➤ Arithmetic Operation
➤ Sum Avg of 3 Number
➤ Area Program in C++
➤ Simple Interest in C++
➤ Find ASCII value in C++
➤ Swap 2 Number in C++
Flow Control Programs
➤ Even-Odd in C++
➤ +ve, -ve, 0 in C++
➤ Vowel-Consonant
➤ Greatest of 3 no.
➤ Check Leap Year
➤ Calculator Program
➤ Reverse a Number
➤ Sum of Natural Number
➤ GCD of 2 Number
➤ LCM of 2 Number
➤ Find Power in C++
➤ Fibonacci Series in C++
➤ Palindrome Number
➤ Find Factorial in C++
➤ Factorial Using Recursion
➤ Prime Number in C++
➤ Prime Number b/w 1-N
Array
➤ Linear Search in C++
➤ Binary Search in C++
Others
➤ Introduction to C++
➤ Data Types in C++
➤ Range of Data Types
➤ Void main, main vs int main
Write a program to find and print the grade of a student by using the switch case statement in C++. The below table shows the grading system.
Score in subject | Grade |
>=90 | A |
80-89 | B |
70-79 | C |
60-69 | D |
50-59 | E |
<50 | F |
C++ Program to Find Grade of Student using Switch Case
// C++ program to Find Grade of Student
#include<iostream>
using namespace std;
int main()
{
// variables
int score;
char grade;
// take score
cout << "Enter score(0-100): ";
cin >> score;
// check score is valid or not
// score is valid if it belongs to 0-100
if(score<0 || score>100)
{
cout << "Invalid Score" << endl;
return 0; // stop execution
}
// find grade using switch-case
switch(score/10)
{
case 10:
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
case 5:
grade = 'E';
break;
default:
grade = 'F';
}
// display grade
cout << "Grade = " << grade << endl;
return 0;
}
Output:-
Enter score(0-100): 92
Grade = A
Enter score(0-100): 50
Grade = E
Enter score(0-100): 25
Grade = F
Enter score(0-100): 142
Invalid Score
Enter score(0-100): -20
Invalid Score
In this program, we have declared score and grade variables. The score variables will be used to store input from the end-user and the grade variable will store the grade value after finding the grade using an if-else statement. Then we have taken the input value from the end-user and stored it in the score variable.
Now, 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 it will display the appropriate message and stop the execution of the program.
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’. Finally, we displayed the grade value on the screen.
Find Grade of Student using Switch Case and Function in C++
// C++ program to Find Grade of Student
#include<iostream>
using namespace std;
// 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
cout << "Enter score(0-100): ";
cin >> score;
// find grade
grade = findGrade(score);
// display grade
if(grade=='\0')
cout << "Invalid Score";
else
cout << "Grade = " << grade << endl;
return 0;
}
In this program while using function, we didn’t used break statement because whenever we use return statement then control came back to the caller method, and the next statements of called function won’t executed.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!