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
- Ask the user for the number of semesters
- For each semester, get the SGPA and credits
- Calculate weighted sum: Σ(SGPA × Credits)
- Calculate total credits: Σ(Credits)
- CGPA = weighted sum / total credits
- 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:
Yes! A basic CGPA calculator in C++ needs: (1) Input SGPAs and credits for each semester. (2) Multiply each SGPA by its credits. (3) Sum all products and divide by total credits. The code example on this page demonstrates exactly this.
Not exactly. The C++ example is a simplified console program for learning purposes. CGPA Path is a full web application with a polished interface, visual progress bars, localStorage persistence, and more features.