In this tutorial, we will write a C++ program to find the grades of the student. Before that, you should have knowledge of the following topics in C++.
The program takes the user input for a number of subjects and the marks obtained on each of the subjects. The marks of the subject is taken using for loop. Then the program finds the sum of the marks and finally the average of the marks.
Then using an else-if ladder in C++, we will find the category of the marks it belongs to. and display the grade obtained by the student.
C++ Program to Calculate Grade of Student
C++ Program to Calculate Grade of Student using if else statement.
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 | #include<iostream> using namespace std; int main() { int num, i; float marks[10], avg = 0, sum = 0; cout << "Enter number of subject: "; cin >> num; cout << "Enter marks for " << num << " subjects:\n"; for (i = 0; i < num; i++) { cin >> marks[i]; } //find the sum and average for (i = 0; i < num; i++) { sum += marks[i]; } avg = sum / num; cout << "Average obtained: " << avg; cout << "\nGrade obtained: "; if (avg > 90) { cout << "A grade"; } else if (avg < 90 && avg >= 75) { cout << "B grade"; } else if (avg < 75 && avg >= 50) { cout << "C grade"; } else if (avg < 50 && avg >= 30) { cout << "D grade"; } else { cout << "Fail"; } return 0; } |
Output:
Enter number of subject: 5
Enter marks for 5 subjects:
75
84
29
63
77
Average obtained: 78.2
Grade obtained: B grade
Although, the range of average for each grade is up to the programmer. You can change the average range and grade according to the program you want to create.