This blog is under construction

Monday, 8 July 2013

C program to calculate GPA

How to calculate GPA?
  • Find the sum of credits of all  the subjects.
  • Find the sum of product of credit and grade point for all the subjects.  Store the result in total.
  • Divide total by sum of credits of all the subjects.

Grade point for S, A, B, C, D, E are 10, 9, 8, 7, 6, 5 correspondingly.

Example:
Grade | Credit
    B    |    3
    E    |    5
   C     |    3
   B     |    4
   C     |    4

Total credits = 19
sum of product of credit and 
     grade points for all the subjects = (8 * 3) + (5 * 5) + (7 * 3) + (8 * 4) + (7 * 4)
                                                   = 130

GPA = (sum of product of credit * grade points for all subjects) / total credits
         = 130 / 19
GPA   = 6.84




Write a C program to calculate GPA.


  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  int main() {
        char str[100], ch;
        int i, grade[5];
        float credit[5], gpa = 0.0, totCredit = 0.0;

        /* get the letter grade and credits from the user */
        printf("Letter Grade and Credits for each subject:\n");
        for (i = 0; i < 5; i++) {
                printf("Subject %d(Grade|Credit):", i + 1);
                ch = getchar();
                grade[i] = ch;
                scanf("%f", &credit[i]);
                getchar();
        }

        /* print the input grades and credits */
        printf("\nSubject | Grade | Credit\n");
        for (i = 0; i < 5; i++) {
                printf("   %d    |  %c    | %.0f\n", i + 1, grade[i], credit[i]);
        }

        /* calculate gpa value */
        for (i = 0; i < 5; i++) {
                switch (grade[i]) {
                        case 'S':
                                gpa = gpa + 10 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;
                        case 'A':
                                gpa = gpa + 9 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'B':
                                gpa = gpa + 8 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'C':
                                gpa = gpa + 7 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'D':
                                gpa = gpa + 6 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        case 'E':
                                gpa = gpa + 5 * credit[i];
                                totCredit = totCredit + credit[i];
                                break;

                        default:
                                printf("Given Wrong grade!!\n");
                                exit(0);

                }
        }
        printf("GPA: %f\tcredit: %f\n", gpa, totCredit);
        gpa = gpa / totCredit;
        printf("GPA for your score: %.2f\n", gpa);
        return 0;
  }




  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Letter Grade and Credits for each subject:
  Subject 1(Grade|Credit):B 3
  Subject 2(Grade|Credit):E 5
  Subject 3(Grade|Credit):C 3
  Subject 4(Grade|Credit):B 4
  Subject 5(Grade|Credit):C 4

  Subject | Grade | Credit
     1    |  B    | 3
     2    |  E    | 5
     3    |  C    | 3
     4    |  B    | 4
     5    |  C    | 4
  GPA: 130.000000 credit: 19.000000
  GPA for your score: 6.84





See Also:

3 comments:

  1. i want a program that i input a number of a subject in a program and that program show me the cgpa and gade point of that number. And this program will be create with function. plz some one help me to do this

    ReplyDelete
  2. Just see {Ali-noor}
    ==================
    #include
    int main()
    {
    int marks;
    printf("Enter Your Number\n");
    scanf("%d",& marks);
    if (marks>=80 && marks<=100)
    {
    printf("A+");
    }
    else if (marks<=79 && marks>=75)
    {
    printf("A");
    }
    else if (marks<=74 && marks>=70)
    {
    printf("A-");
    }
    else if (marks<=69 && marks>=65)
    {
    printf("B+");
    }
    else if (marks<=64 && marks>=60)
    {
    printf("B-");
    }
    else if (marks<=59 && marks>=55)
    {
    printf("C+");
    }
    else if (marks<=54 && marks>=50)
    {
    printf("C");
    }
    else if (marks<=49 && marks>=45)
    {
    printf("C-");
    }
    else if (marks<=44 && marks>=40)
    {
    printf("D");
    }
    else

    printf("F");
    return 0;
    }

    ReplyDelete