C++ Program to Calculate Sum of 5 Subjects and Find Percentage and Grade
C++ program to calculate sum of 5 subjects and find percentage and grade.
In this article, you will learn how to make a C++ program to calculate sum of 5 subjects and find percentage and grade.
Example
Enter the marks of five subjects::
95
85
74
64
53
The Total marks = 371/500
The Average marks = 74.2
The Percentage = 74.2%
The Grade = 'C'
You should have the knowledge of following these topics in c++ programming to understand this example:
- C++
main()
function - C++
cin
object - C++
cout
object - C++
If-else
Statement - C++
Switch
Statement - C++ Functions
Standard Formula
Total = marks1 + marks2 + marks3 + marks4 + marks5
Average = Total / 5.0
Percentage = (Total / 500.0) x 100
Where marks1
, marks2
, marks3
, marks4
, and marks5
are the marks of five subjects.
In this article, we solved this problem in four methods:
- Using the if-else statement
- Using the switch case statement
- Using the functions
- Using the nested if-else
C++ Program to Find Grade of a Student using If-else
// 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 sub_1, sub_2, sub_3, sub_4, sub_5;
float total = 0.00, average = 0.00, percentage = 0.00;
char grade;
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 calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// 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;
}
Output
Enter the marks of five subjects::
95
85
74
64
53
The Total marks = 371/500
The Average marks = 74.2
The Percentage = 74.2%
The Grade = 'C'
C++ Program to Find Grade of a Student using Switch Statement
// 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;
}
Explanation
Average: we take the marks of five subjects as input after that the sum of these marks is divided by 5
then It will return the average value of the marks.
Percentage: we made a division of the sum of user's marks by 500
then It multiplied by 100
then It will return the percentage value of the marks.
Grade: we compared the value of the Average marks with this range of marks to find the Grade of students like:
If Average marks >= 90
then Its Grade is 'A'
If Average marks >= 80 and < 90
then Its Grade is 'B'
If Average marks >= 70 and < 80
then Its Grade is 'C'
If Average marks >= 60 and < 70
then Its Grade is 'D'
If Average marks < 60
then Its Grade is 'E'
C++ Program to Find Grade of a Student using Function
// C++ Program to Find Grade of a Student using Function
#include <bits/stdc++.h>
using namespace std;
// This function will display the percentage, average, and grade
void CalcPercentGrade(float sub_1, float sub_2, float sub_3, float sub_4, float sub_5) {
float total = 0.00, average = 0.00, percentage = 0.00;
char grade;
// 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 calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// 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";
}
// It's the driver function
int main() {
// To store the values of five subjects
float sub_1, sub_2, sub_3, sub_4, sub_5;
cout << "Enter the marks of five subjects::\n";
cin >> sub_1 >> sub_2 >> sub_3 >> sub_4 >> sub_5;
// It will produce the final output
CalcPercentGrade(sub_1, sub_2, sub_3, sub_4, sub_5);
return 0;
}
Enter the marks of five subjects::
98
87
89
88
79
The Total marks = 441/500
The Average marks = 88.2
The Percentage = 88.2%
The Grade = 'B'
You should also read these things
C Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student
C++ Program to Find Grade of a Student using Nested If-else
// C++ Program to Find 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 sub_1, sub_2, sub_3, sub_4, sub_5;
float total = 0.00, average = 0.00, percentage = 0.00;
char grade;
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 calculate the Grade
if (average >= 90) {
grade = 'A';
}
else {
if (average >= 80 && average < 90) {
grade = 'B';
}
else {
if (average >= 70 && average < 80) {
grade = 'C';
}
else {
if (average >= 60 && average < 70) {
grade = 'D';
}
else {
grade = 'E';
}
}
}
}
// 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;
}