-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgpa.c
More file actions
48 lines (39 loc) · 1.28 KB
/
Copy pathcgpa.c
File metadata and controls
48 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
float calculateCGPA(float grades[], int creditHours[], int numCourses) {
float totalCreditHours = 0;
float totalGradePoints = 0;
for (int i = 0; i < numCourses; i++) {
float gradePoints;
if (grades[i] == 'A') {
gradePoints = 4.0;
} else if (grades[i] == 'B') {
gradePoints = 3.0;
} else if (grades[i] == 'C') {
gradePoints = 2.0;
} else if (grades[i] == 'D') {
gradePoints = 1.0;
} else {
gradePoints = 0.0;
}
totalCreditHours += creditHours[i];
totalGradePoints += gradePoints * creditHours[i];
}
float cgpa = totalGradePoints / totalCreditHours;
return cgpa;
}
int main() {
int numCourses;
printf("Enter the number of courses: ");
scanf("%d", &numCourses);
float grades[numCourses];
int creditHours[numCourses];
for (int i = 0; i < numCourses; i++) {
printf("Enter grade for course %d (A/B/C/D): ", i+1);
scanf(" %c", &grades[i]);
printf("Enter credit hours for course %d: ", i+1);
scanf("%d", &creditHours[i]);
}
float cgpa = calculateCGPA(grades, creditHours, numCourses);
printf("Your CGPA is: %.2f\n", cgpa);
return 0;
}