Skip to main content

How to Make a CGPA Calculator in C++

Last updated: June 2025 · 7 min read

Building a CGPA calculator is a common C++ assignment for engineering students. This guide walks through the logic, formula, and complete code example.

The Logic

  1. Ask the user for the number of semesters
  2. For each semester, get the SGPA and credits
  3. Calculate weighted sum: Σ(SGPA × Credits)
  4. Calculate total credits: Σ(Credits)
  5. CGPA = weighted sum / total credits
  6. Display the result

Complete C++ Code

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n;
    cout << "===== CGPA Calculator =====" << endl;
    cout << "Enter number of semesters: ";
    cin >> n;

    if (n <= 0) {
        cout << "Invalid number of semesters." << endl;
        return 1;
    }

    double totalWeighted = 0.0;
    int totalCredits = 0;

    for (int i = 1; i <= n; i++) {
        double sgpa;
        int credits;

        cout << endl << "Semester " << i << ":" << endl;
        cout << "  SGPA (0-10): ";
        cin >> sgpa;
        cout << "  Credits: ";
        cin >> credits;

        // Validate input
        if (sgpa < 0 || sgpa > 10 || credits <= 0) {
            cout << "  Invalid input! SGPA must be 0-10, credits > 0." << endl;
            i--; // Retry this semester
            continue;
        }

        totalWeighted += sgpa * credits;
        totalCredits += credits;
    }

    double cgpa = totalWeighted / totalCredits;
    double percentage = cgpa * 9.5;

    cout << endl << "===== Results =====" << endl;
    cout << fixed << setprecision(2);
    cout << "Your CGPA: " << cgpa << endl;
    cout << "Percentage (approx): " << percentage << "%" << endl;
    cout << "Total Credits: " << totalCredits << endl;

    return 0;
}

Sample Output

===== CGPA Calculator =====
Enter number of semesters: 4

Semester 1:
  SGPA (0-10): 8.2
  Credits: 22

Semester 2:
  SGPA (0-10): 8.5
  Credits: 24

Semester 3:
  SGPA (0-10): 8.8
  Credits: 20

Semester 4:
  SGPA (0-10): 8.6
  Credits: 22

===== Results =====
Your CGPA: 8.52
Percentage (approx): 80.91%
Total Credits: 88

How It Works

  • Weighted average: Each semester's SGPA is multiplied by its credit hours. Semesters with more credits count more in the final CGPA.
  • Input validation: The code checks that SGPA is between 0 and 10 and credits are positive. Invalid entries prompt a retry.
  • Percentage conversion: Uses the standard Indian formula (CGPA × 9.5) for approximate percentage.

Simple Version (Without Credits)

If you want a simpler version that just averages SGPAs:

#include <iostream>
using namespace std;

int main() {
    int n;
    double sum = 0;
    cout << "Enter number of semesters: ";
    cin >> n;

    for (int i = 1; i <= n; i++) {
        double sgpa;
        cout << "SGPA for Semester " << i << ": ";
        cin >> sgpa;
        sum += sgpa;
    }

    cout << "Your CGPA: " << sum / n << endl;
    return 0;
}

Use Our Online Calculator Instead

Don't want to compile C++ code? Use our free online calculators: