In this C++ program, we will calculate the grade of a student based on the total marks obtained by the student in five subjects. Here we will use a switch case statement, however you can write same program using if-else ladder statement also. Here is the range of Grades:
Marks >= 90 : Grade A Marks >= 70 && < 90 : Grade B Marks >= 50 && < 70 : Grade C Marks < 50 : Grade D
We will first ask user to enter the marks of five subjects and calculate the total marks obtained by student. Then we will calculate the average marks by dividing total marks by 5. Now, we will use switch case statement to select the appropriate range for his average marks and print the grade accordingly.
C++ Program to Calculate Grade of Student Using Switch Statement
#include <iostream> using namespace std; int main() { int score, i, average; float total=0; cout<< "Enter marks of 5 subjects\n"; for(i=0; i<5; i++) { cin >> score; total += score; } average = total/5; cout<<"Grade : "; switch(average/10) { case 9 : cout << "A"; break; case 8 : case 7 : cout << "B"; break; case 6 : case 5 : cout << "C"; break; default : cout << "D"; } return 0; }Output
Enter marks of 5 subjects 97 89 78 87 68 Grade : B
Recommended Posts