 # C++ Program to Calculate the grade of the student using a user-defined function

Here, we are discussing a program to find the Grade of a Student based on Marks obtained in all Subjects using a user-defined function. Here the user is allowed to enter the number of subjects
The grade must be calculated based on the following pattern:
91-100    A1
81-90    A2
71-80    B1
61-70    B2
51-60    C1
41-50    C2
33-40    D
21-32    E1
0-20    E2

## How do calculate the grade of a student?

To calculate the grade of a student based on total marks. The grade is calculated based on the total marks obtained for n subjects. Add marks of all the n subjects and divide it by n to get the average mark. And based on this average mark, find the grade as per the table given above:

## C++ program to calculate the grade of a student

Here the program asks the user to enter the number of the subject. For example, if he/she enters 3 as the number of subjects, then the program further asks to enter marks obtained in 3 subjects.
This program calculates the grade of the student using a user-defined function
Get the number of subjects into the variable n and get the marks of n subjects to the array mark; pass the array of marks and the number of subjects to a user-defined function that says findGrade( float mark[ ], int n );
Defining the function findGrade (float [ ], int)
First, we have to add all the marks for n subjects. For this, get each subject’s mark and add them together.

``````
float sum=0;
for(i=0; i
``````

```Now we have to divide the total mark by the number of subjects to get the average. avg = sum/n; Now we have the average mark in the variable avg. Compare the value with the average mark range.```

``````

if(avg>=91 && avg<=100)

return "A1";

else if(avg>=81 && avg<91)

return "A2";

else if(avg>=71 && avg<81)

return "B1";

else if(avg>=61 && avg<71)

return "B2";

else if(avg>=51 && avg<61)

return "C1";

else if(avg>=41 && avg<51)

return "C2";

else if(avg>=33 && avg<41)

return "D";

else if(avg>=21 && avg<33)

return "E1";

else if(avg>=0 && avg<21)

return "E2";

else

return "Invalid!"

``````

`get the return value of the function and display the grade.`

### `Algorithm`

`Step 1: Call the header file iostream.`

`Step 2: Use the namespace std.`

`Step 3: Declare a user defined function const char* findGrade(float [], int);`

`Step 4: Open the integer type main function; int main().`

`Step 5: Declare integer type variables I, n. float type array mark;`

`Step 6: Ask the user to enter the number of subjects`

`Step 7:Get the number into the variable n;`

`Step 8: Ask the user to enter the marks obtained;`

`Step 9: Get the numbers into the array mark;`

`Step 10: call step: 13 and get the grade `

`Step 11:Print the grade; `

`Step 12: exit;`

```Step 13: const char* findGrade(float mark[], int n)               Calculate the total marks and get the result into a variable sum;               Calculate the average and get the result into the variable avg;               Compare the value of avg with the average mark range and return the                                 corresponding grade;```

` `

## C++ Source Code

```                                          ```#include<iostream>
using namespace std;
int main()
{
int i, n;
float mark;
cout<<"Enter Number of Subjects (max. 10): ";
cin>>n;
cout<<"Enter Marks obtained in "<<n<<" Subjects: ";
for(i=0; i<n; i++)
cin>>mark[i];
cout<<endl;
return 0;
}
const char* findGrade(float mark[], int n)
{
int i;
float sum=0, avg;
for(i=0; i<n; i++)
sum = sum+mark[i];
avg = sum/n;
if(avg>=91 && avg<=100)
return "A1";
else if(avg>=81 && avg<91)
return "A2";
else if(avg>=71 && avg<81)
return "B1";
else if(avg>=61 && avg<71)
return "B2";
else if(avg>=51 && avg<61)
return "C1";
else if(avg>=41 && avg<51)
return "C2";
else if(avg>=33 && avg<41)
return "D";
else if(avg>=21 && avg<33)
return "E1";
else if(avg>=0 && avg<21)
return "E2";
else
return "Invalid!";
}```
```

## OUTPUT

```Enter Number of Subjects (max. 10): 6
Enter Marks obtained in 6 Subjects: 69
80
55
74
45
91