C++ Mini Projects: Student Grade Calculator
Hey everyone, welcome back to the series. Remember we built a simple calculator back in the series, now this is going to be a bit advance version of that calculator. So make sure you haven't missed that blog before starting to understand and work on this one.
Here is what its going to do: Input marks for different subjects, calculate the average, and display the grade. This project enables the user in
. Inputting marks for all subjects
. calculation of average marks
. assignment of grades based on marks
Here is a short example:
Grade A >=90
Grade B>=80
Grade C>=70
Grade D>=60
Grade F<60
Skills you will learn throughout: Conditional statements and arithmetic operations.
Here is the go to build the desired project:
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numSubjects;
vector<double> marks;
double total = 0, average = 0;
char grade;
// Input the number of subjects
cout << "Student Grade Calculator\n";
cout << "------------------------\n";
cout << "Enter the number of subjects: ";
cin >> numSubjects;
// Input marks for each subject
for (int i = 0; i < numSubjects; ++i) {
double mark;
cout << "Enter marks for subject " << i + 1 << ": ";
cin >> mark;
marks.push_back(mark); // Add mark to the vector
total += mark; // Add mark to the total
}
// Calculate average
average = total / numSubjects;
// Determine grade
if (average >= 90) {
grade = 'A';
} else if (average >= 80) {
grade = 'B';
} else if (average >= 70) {
grade = 'C';
} else if (average >= 60) {
grade = 'D';
} else {
grade = 'F';
}
// Display results
cout << "\nResults:\n";
cout << "Total Marks: " << total << endl;
cout << "Average Marks: " << average << endl;
cout << "Grade: " << grade << endl;
return 0;
}
How the code works?
marks.push_back(mark)
Storing marks in vector
Total ...
Keeps the sum of all marks that are entered
Calculation of average...
average = total / numSubjects;
Division of total marks by number of subjects to obtain average
Assignment of grades....
if (average >= 90) {
grade = 'A';
} else if (average >= 80) {
grade = 'B';
} else if (average >= 70) {
grade = 'C';
} else if (average >= 60) {
grade = 'D';
} else {
grade = 'F';
}
Using else if statements for comparing average marks
Assignment of grades based on average marks
Displaying result...
cout << "Total Marks: " << total << endl;
cout << "Average Marks: " << average << endl;
cout << "Grade: " << grade << endl;
Displays average marks and grades accordingly
Input
Enter the number of subjects: 5
Enter marks for subject 1: 85
Enter marks for subject 2: 78
Enter marks for subject 3: 92
Enter marks for subject 4: 88
Enter marks for subject 5: 76
Output
Results:
Total Marks: 419
Average Marks: 83.8
Grade: B
So you can see our program is working fine now and deals with all kind of calculations of average and effectively gives the grade.
Thanks for being with me and I will see you next time.
Comments
Post a Comment