C++ Online Compiler
Example: C++ Program to Find Grade of a Student using Switch Statement
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// C++ Program to Find Grade of a Student using Switch Statement #include <bits/stdc++.h> using namespace std; int main() { // To store the values of five subjects float sub_1, sub_2, sub_3, sub_4, sub_5; float total = 0.00, average = 0.00, percentage = 0.00; char grade; int sAvg; cout << "Enter the marks of five subjects::\n"; cin >> sub_1 >> sub_2 >> sub_3 >> sub_4 >> sub_5; // It will calculate the Total, Average and Percentage total = sub_1 + sub_2 + sub_3 + sub_4 + sub_5; average = total / 5.0; percentage = (total / 500.0) * 100; // It will divides average by 10 check switch expression sAvg = (int) average / 10; // It will calculate the Grade switch (sAvg) { case 10: grade = 'A'; break; case 9: grade = 'A'; break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; default: grade = 'E'; break; } // It will produce the final output cout << "\nThe Total marks = " << total << "/500\n"; cout << "The Average marks = " << average << "\n"; cout << "The Percentage = " << percentage << "%\n"; cout << "The Grade = '" << grade << "'\n"; return 0; }
95 85 74 64 53
Output
Clear
ADVERTISEMENTS