Write a C++ program to Show grades with marks percentage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> using namespace std; int main() { float marks; float percentage; // Prompt the user to enter marks cout << "Enter total marks obtained: "; cin >> marks; // Calculate percentage percentage = (marks / 100) * 100; // Assuming total marks are 100 for simplicity // Determine grade based on percentage switch (static_cast<int>(percentage / 10)) { case 10: // For 100% case 9: cout << "Grade: A+" << endl; break; case 8: cout << "Grade: A" << endl; break; case 7: cout << "Grade: B" << endl; break; case 6: cout << "Grade: C" << endl; break; case 5: cout << "Grade: D" << endl; break; case 4: cout << "Grade: E" << endl; break; default: cout << "Grade: F" << endl; // For marks below 40 break; } return 0; } |
Example of all possible Outputs:
Example Output #1
Enter total marks obtained:
85
Grade: A
Example Output #2
Enter total marks obtained:
95
Grade: A+
Example Output #3
Enter total marks obtained:
70
Grade: B
Example Output #4
Enter total marks obtained:
60
Grade: C
Example Output #5
Enter total marks obtained:
45
Grade: D
Example Output #6
Enter total marks obtained:
30
Grade: F
Example#2
Let us see the C++ program to Calculate the percentage of marks and show grade using the switch statements. Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
Flowchart of the program to Calculate the percentage of marks and show the grade
Program in C++ to Calculate the percentage of marks and show the grade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# include <iostream> # include <conio.h> using namespace std; int main() { int s1,s2,s3,s4,s5; float per; char grade; cout<<"enter marks of five subjects"<<endl; cin>>s1>>s2>>s3>>s4>>s5; per=((s1+s2+s3+s4+s5)*100)/500; cout<<per<<endl; switch (per>=90&&per<100) { case 1: cout<<"your grade is A"; break; case 0: switch(per>=80 &&per<90) { case 1: cout<<"grade is B"; break; case 0: switch(per>=70&&per<80) { case 1: cout<<"your grade is C"; break; case 0: switch (per>=60&&per<70) { case 1: cout<<"grade is D"; break; case 0: switch
(per>=50&&per<60) { case 1: cout<<"grade is E"; break; case 0: cout<<"grade is F"; break; } break; } break; } break; } break; } } |
Output
SFT (Shamil’s Flow Table )
Are you interested to Read about SFT(Shamil’s Flow Table)?
C++ Program to input marks of subjects and show percentage, and grade without a switch statement
Click Here Read More about this Shamil’s Memory Table of grade program |
C++ program to find percentage of marks and show grade using if else statements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// C++ Program to Calculate Sum of 5 Subjects and Find Percentage and Grade #include <bits/stdc++.h> using namespace std; int main() { // To store the values of five subjects float Subject1, Subject2, Subject3, Subject4; float Total_Marks = 0.00, Average_Marks = 0.00, percentage = 0.00; char grade; cout << "Enter the marks of four subjects::\n"; cin >> Subject1 >> Subject2 >> Subject3 >> Subject4; // It will calculate the Total_Marks, Average_Marks and Percentage Total_Marks = Subject1 + Subject2 + Subject3 + Subject4; Average_Marks = Total_Marks / 4.0; percentage = (Total_Marks / 400.0) * 100; // It will calculate the Grade if (Average_Marks >= 90) grade = 'A'; else if (Average_Marks >= 80 && Average_Marks < 90) grade = 'B'; else if (Average_Marks >= 70 && Average_Marks < 80) grade = 'C'; else if (Average_Marks >= 60 && Average_Marks < 70) grade = 'D'; else grade = 'E'; // It will produce the final output cout << "\n The Total_Marks marks = " << Total_Marks << "/400\n"; cout << "The Average_Marks marks = " << Average_Marks << "\n"; cout << "The Percentage = " << percentage << "%\n"; cout << "The Grade = '" << grade << "'\n"; return 0; } |
Output
Enter the marks of four subjects::
50
60
60
50
The Total_Marks marks = 220/400
The Average_Marks marks = 55
The Percentage = 55%
The Grade = ‘E’
——————————–
C++ program to find percentage of marks and show grade using Nested if else statements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
// C++ Program to Find Student_Grade of a Student using Nested If-else #include <bits/stdc++.h> using namespace std; int main() { // To store the values of five subjects float Subject1, Subject2, Subject3, Subject4; float Total_Marks = 0.00, Average_Marks = 0.00, percentage = 0.00; char Student_Grade; cout << "Enter the marks of five subjects::\n"; cin >> Subject1 >> Subject2 >> Subject3 >> Subject4; // It will calculate the Total_Marks, Average_Marks and Percentage Total_Marks = Subject1 + Subject2 + Subject3 + Subject4; Average_Marks = Total_Marks / 4.0; percentage = (Total_Marks / 400.0) * 100; // It will calculate the Student_Grade if (Average_Marks >= 90) { Student_Grade = 'A'; } else { if (Average_Marks >= 80 && Average_Marks < 90) { Student_Grade = 'B'; } else { if (Average_Marks >= 70 && Average_Marks < 80) { Student_Grade = 'C'; } else { if (Average_Marks >= 60 && Average_Marks < 70) { Student_Grade = 'D'; } else { Student_Grade = 'E'; } } } } // It will produce the final output cout << "\nThe Total_Marks marks = " << Total_Marks << "/400\n"; cout << "The Average_Marks marks = " << Average_Marks << "\n"; cout << "The Percentage = " << percentage << "%\n"; cout << "The Student_Grade = '" << Student_Grade << "'\n"; return 0; } |
Output
Enter the marks of four subjects::
40
40
60
60
The Total_Marks marks = 200/400
The Average_Marks marks = 50
The Percentage = 50%
The Student_Grade = ‘E’
——————————–
C++ program to find percentage of marks and show grade using User define function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
// C++ Program to Find Grade of a Student using Function #include <bits/stdc++.h> using namespace std; // This function will display the percentage, Marks_Average, and grade void CalcPercentGrade(float Subject1, float Subject2, float Subject3, float Subject4) { float Total_Marks = 0.00, Marks_Average = 0.00, percentage = 0.00; char grade; // It will calculate the Total_Marks, Marks_Average and Percentage Total_Marks = Subject1 + Subject2 + Subject3 + Subject4; Marks_Average = Total_Marks / 4.0; percentage = (Total_Marks / 400.0) * 100; // It will calculate the Grade if (Marks_Average >= 90) grade = 'A'; else if (Marks_Average >= 80 && Marks_Average < 90) grade = 'B'; else if (Marks_Average >= 70 && Marks_Average < 80) grade = 'C'; else if (Marks_Average >= 60 && Marks_Average < 70) grade = 'D'; else grade = 'E'; // It will produce the final output cout << "\nThe Total_Marks marks = " << Total_Marks << "/400\n"; cout << "The Marks_Average marks = " << Marks_Average << "\n"; cout << "The Percentage = " << percentage << "%\n"; cout << "The Grade = '" << grade << "'\n"; } // It's the driver function int main() { // To store the values of four subjects float Subject1, Subject2, Subject3, Subject4; cout << "Enter the marks of four subjects::\n"; cin >> Subject1 >> Subject2 >> Subject3 >> Subject4; // It will produce the final output CalcPercentGrade(Subject1, Subject2, Subject3, Subject4); return 0; } |
Output
Enter the marks of four subjects::
40
40
60
60
The Total_Marks marks = 200/400
The Average_Marks marks = 50
The Percentage = 50%
The Student_Grade = ‘E’