0% found this document useful (0 votes)
2 views

Program

The document contains a C++ program that calculates the Cumulative Grade Point Average (CGPA) based on user input for multiple courses. It defines a struct for courses, collects course details including name, credits, and grade, and computes the CGPA using a function. The program outputs the calculated CGPA formatted to two decimal places.

Uploaded by

srcramchand
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program

The document contains a C++ program that calculates the Cumulative Grade Point Average (CGPA) based on user input for multiple courses. It defines a struct for courses, collects course details including name, credits, and grade, and computes the CGPA using a function. The program outputs the calculated CGPA formatted to two decimal places.

Uploaded by

srcramchand
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PROGRAM:

#include <iostream>

#include <vector>

#include <iomanip>

using namespace std;

struct Course {

string name;

int credits;

float grade;

};

float calculateCGPA(const vector<Course>& courses) {

float totalGradePoints = 0.0;

int totalCredits = 0;

for (const auto& course : courses) {

totalGradePoints += course.grade * course.credits;

totalCredits += course.credits;

return totalCredits > 0 ? totalGradePoints / totalCredits : 0.0;

int main() {

int numCourses;

cout << "Enter the number of courses: ";

cin >> numCourses;

vector<Course> courses(numCourses);
for (int i = 0; i < numCourses; ++i) {

cout << "Enter the name of course " << (i + 1) << ": ";

cin >> courses[i].name;

cout << "Enter the credits for " << courses[i].name << ": ";

cin >> courses[i].credits;

cout << "Enter the grade for " << courses[i].name << ": ";

cin >> courses[i].grade;

float cgpa = calculateCGPA(courses);

cout << fixed << setprecision(2);

cout << "Cumulative Grade Point Average (CGPA): " << cgpa << endl;

return 0;

You might also like