Calculate the CGPA and CGPA % of marks obtained by a Student in N subjects - GeeksforGeeks

10 Nov, 2021
Given an array arr[] of size N which contains the marks of a student in N subjects, the task is to calculate the CGPA and the CGPA percentage of the student. Note: Consider all marks to be out of 100, for each subject.
CGPA(Cumulative Grade Point Average) is the systematic arrangement in the educational stream to get an average of grade points.
Examples:
Input: arr[] = {90, 80, 70, 80, 90} Output: CGPA = 8.2, Percentage = 77.89 Explanation: The grade in each subject respectively out of 10 is {9, 8, 7, 8, 9}. The CGPA is the average of all the grades = (9 + 8 + 7 + 8 + 9) / 5 = 8.2 The percentage of this CGPA is 77.89.
Input: arr[] = {90, 90, 90, 80, 85} Output: CGPA = 8.7, Percentage = 82.65
Approach: In this article, the CGPA is calculated on a scale of 10.
Input the array from the user containing the marks of the student in N subjects.
Since the scale is on 10, divide marks in every subject by 10 to find the GPA of the student in each subject.
The average of all the GPA’s yield the overall CGPA of the student.
After finding the CGPA, the CGPA percentage can be calculated by the formula:
CGPA% = CGPA * 9.5
This is a general formula for a scale of 10. However, if the entire calculation is made on a scale of 4, this 9.5 is thereby multiplied by 2.5 and the CGPA percentage is found by multiplying with 23.75.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program to calculate the CGPA// and CGPA percentage of a student#include<bits/stdc++.h>using namespace std; double CgpaCalc(double marks[], int n){    // Variable to store the grades in    // every subject    double grade[n];     // Variables to store CGPA and the    // sum of all the grades    double cgpa, sum = 0;     // Computing the grades    for(int i = 0; i < n; i++)    {       grade[i] = (marks[i] / 10);    }     // Computing the sum of grades    for(int i = 0; i < n; i++)    {       sum += grade[i];    }     // Computing the CGPA    cgpa = sum / n;     return cgpa;} // Driver codeint main(){    int n = 5;    double marks[] = { 90, 80, 70, 80, 90 };     double cgpa = CgpaCalc(marks, n);             cout << "CGPA = ";    printf("%.1f\n", cgpa);    cout << "CGPA Percentage = ";    printf("%.2f", cgpa * 9.5);} // This code is contributed by Bhupendra_Singh
// Java program to calculate the CGPA// and CGPA percentage of a student import java.util.Scanner;class CGPA {     public static double CgpaCalc(double[] marks, int n)    {        // Variable to store the grades in        // every subject        double grade[] = new double[n];         // Variables to store CGPA and the        // sum of all the grades        double cgpa, sum = 0;         // Computing the grades        for (int i = 0; i < n; i++) {            grade[i] = (marks[i] / 10);        }         // Computing the sum of grades        for (int i = 0; i < n; i++) {            sum += grade[i];        }         // Computing the CGPA        cgpa = sum / n;         return cgpa;    }     // Driver code    public static void main(String args[])    {         int n = 5;        double[] marks            = { 90, 80, 70, 80, 90 };         double cgpa = CgpaCalc(marks, n);         System.out.println(            "CGPA = " + cgpa);        System.out.println(            "CGPA Percentage = "            + String.format("%.2f", cgpa * 9.5));    }}
# Python3 program to calculate the CGPA# and CGPA percentage of a studentdef CgpaCalc(marks, n):     # Variable to store the grades in    # every subject    grade = [0] * n       # Variables to store CGPA and the    # sum of all the grades    Sum = 0       # Computing the grades    for i in range(n):       grade[i] = (marks[i] / 10)       # Computing the sum of grades    for i in range(n):       Sum += grade[i]       # Computing the CGPA    cgpa = Sum / n       return cgpa   # Driver coden = 5marks = [ 90, 80, 70, 80, 90 ] cgpa = CgpaCalc(marks, n)       print("CGPA = ", '%.1f' % cgpa)print("CGPA Percentage = ", '%.2f' % (cgpa * 9.5)) # This code is contributed by divyeshrabadiya07
// C# program to calculate the CGPA// and CGPA percentage of a studentusing System; class GFG{ public static double CgpaCalc(double[] marks,                              int n){         // Variable to store the grades in    // every subject    double []grade = new double[n];     // Variables to store CGPA and the    // sum of all the grades    double cgpa, sum = 0;     // Computing the grades    for(int i = 0; i < n; i++)    {        grade[i] = (marks[i] / 10);    }     // Computing the sum of grades    for(int i = 0; i < n; i++)    {        sum += grade[i];    }     // Computing the CGPA    cgpa = sum / n;     return cgpa;} // Driver codepublic static void Main(String []args){    int n = 5;    double[] marks = { 90, 80, 70, 80, 90 };    double cgpa = CgpaCalc(marks, n);     Console.WriteLine("CGPA = " + cgpa);    Console.WriteLine("CGPA Percentage = {0:F2}",                      cgpa * 9.5);}} // This code is contributed by Amit Katiyar
<script> // Javascript program to calculate the CGPA// and CGPA percentage of a student function CgpaCalc( marks, n){          // Variable to store the grades in    // every subject    let grade = Array.from({length: n}, (_, i) => 0);      // Variables to store CGPA and the    // sum of all the grades    let cgpa, sum = 0;      // Computing the grades    for(let i = 0; i < n; i++)    {        grade[i] = (marks[i] / 10);    }      // Computing the sum of grades    for(let i = 0; i < n; i++)    {        sum += grade[i];    }      // Computing the CGPA    cgpa = sum / n;      return cgpa;} // Driver Code         let n = 5;        let marks            = [ 90, 80, 70, 80, 90 ];          let cgpa = CgpaCalc(marks, n);          document.write(            "CGPA = " + cgpa + "<br/>");        document.write(            "CGPA Percentage = " + (cgpa * 9.5).toFixed(2));        </script>
CGPA = 8.2
CGPA Percentage = 77.90
Time Complexity: O(n)
Auxiliary Space: O(n)
bgangwar59
amit143katiyar
divyeshrabadiya07
code_hunt
subham348
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
C++ Classes and Objects