×

GPA Calculator in C

GPA stands for Grade Point Average. This GPA is used to measure the student's academic performance in educational institutes. By using this, segregation takes place and lets us know how much improvement is needed for the student.

GPA calculation process mathematically

  1. First, we must initialize grade points for certain grades like A+=10, A=9, B+=8, B=7, D+=6, D=5, and F=4.
  2. Then give the grades for the individual subjects that you have.
  3. Take the sum of the grade points and divide this by the number of subjects that you have.
  4. Then we are going to get GPA for the entire subjects that you have.

Program:

//Calculating GPA for 5 subjects
#include<stdio.h>
int main(){
	int mark1, mark2, mark3, mark4, mark5, sum;
	float avg, per;
	printf("enter marks for subject 1 out of 100: ");
	scanf("%d", &mark1);
	printf("enter marks for subject 2 out of 100: ");
	scanf("%d", &mark2);
	printf("enter marks for subject 3 out of 100: ");
	scanf("%d", &mark3);
	printf("enter marks for subject 4 out of 100: ");
	scanf("%d", &mark4);
	printf("enter marks for subject 5 out of 100: ");
	scanf("%d", &mark5);
	sum=mark1+mark2+mark3+mark4+mark5;
	printf("obtained sum is: %d", sum);
	avg=sum/5;
	printf("\nAverage=%0.2f",avg);
	per=(sum*100)/500;
	printf("\nPercentage=%0.4f", per);
	if (per>=90 && per<=100)
	{
		printf("\nGPA = 4.0");
		printf("\nGrade:A+");
		
	}
	else if(per>=85 && per<=89)
	{
		printf("\nGPA = 3.7");
		printf("\nGrade:A");
		
	}
	else if(per>=80 && per<=84)
	{
		printf("\nGPA = 3.3");
		printf("\nGrade:B+");
		
	}
	else if(per>=75 && per<=79)
	{
		printf("\nGPA = 3");
		printf("\nGrade:B");
		
	}
	else if(per>=70 && per<=74)
	{
		printf("\nGPA = 2.7");
		printf("\nGrade:B-");
		
	}
	else if(per>=65 && per<=69)
	{
		printf("\nGPA = 2.3");
		printf("\nGrade:C+");
		
	}
	else if(per>=60 && per<=64)
	{
		printf("\nGPA = 2.0");
		printf("\nGrade:C-");
		
	}
	else if(per>=55 && per<=59)
	{
		printf("\nGPA = 1.7");
		printf("\nGrade:D+");
		
	}
	else if(per>=50 && per<=54)
	{
		printf("\nGPA = 1.3");
		printf("\nGrade:D-");
		
	}
	else if(per>=0 && per<=50)
	{
		printf("\nGPA = 0.0");
		printf("\nGrade:F");
		
	}
	


	
}

Output:

GPA Calculator in C

An explanation for the Program:

We used #include<stdio.h> to import standard input and output functions, which are used to take the input from the user and provide output for the user and guide the user on what to enter and prevent the mistake from popping the error. We also make use of comments which is usually text which is not going to be executed by the compiler. C language uses a compiler to convert one form programming language to another form programming language.

A computer or a system does not really read the characters, but it treats them as a symbol and converts them into binary code. We initialized five variables which are mark1, mark2, mark3, mark4, and mark5, used to take input of marks for five subjects. We also initialized three other variables in float datatype, which is used to initialize decimal values, which may be negative or positive, that are avg, and per. Here avg is the average of five marks that are taken as input, and per is the percentage calculating variable which is going to perform a mathematical operation in the variable.

Now we are going to use conditional statements if, else if, else in which we are going to write what to print in the terminal. Every time we have to write if as the first conditional statement and else as the conditional closing statement, at each interval of the percentage values, we are going to print the grade and the GPA.

Application of the GPA calculator:

  1. In educational institutes like school, universities and colleges, etc., we are going to use the GPA calculator to calculate the GPA for multiple students and that reduces the time for releasing results.
  2. The government uses this GPA calculator program in their special software that performs mathematical processes by which we can get results of GPA and grades.

Another approach for programming a GPA calculator:

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


  int main() {
        char str[100], chr;
        int grade[5], i;
        float gradepoints[5], totalgradepoint = 0.0, gpa = 0.0;
        printf("Enter the grade in capital letters and gradepoints in integer\n");
        for (i = 0; i < 5; i++) {
                printf("Subject %d(grade-gradepoints):", i + 1);
                chr= getchar();
                grade[i] = chr;
                scanf("%f", &gradepoints[i]);
                getchar();
        }


        printf("\nSubject - Grade - Gradepoint\n");
        for (i = 0; i < 5; i++) {
                printf("   %d    -  %c    -  %.0f\n", i + 1, grade[i], gradepoints[i]);
        }


        for (i = 0; i < 5; i++) {
                switch (grade[i]) {
                        case 'S':
                                gpa = gpa + 10 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;
                        case 'A':
                                gpa = gpa + 9 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;


                        case 'B':
                                gpa = gpa + 8 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;


                        case 'C':
                                gpa = gpa + 7 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;


                        case 'D':
                                gpa = gpa + 6 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;


                        case 'E':
                                gpa = gpa + 5 * gradepoints[i];
                                totalgradepoint = totalgradepoint + gradepoints[i];
                                break;


                        default:
                                printf("wrong grade\n");
                                exit(0);


                }
        }
        printf("GPA: %f\tgradepoint: %f\n", gpa, totalgradepoint);
        gpa = gpa / totalgradepoint;
        printf("Total GPA for your score: %0.2f\n", gpa);
        return 0;
  }

Output:

GPA Calculator in C

An explanation for the above Program:

In the above code, we used the switch concept, which replaces conditional statements and acts the same as conditional statements. We have to enter a grade and grade points, we are entering for five subjects, and we used for loop to take string value and integer value to enter the data, which makes code more efficient to work.


Related Topics

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Two-Dimensional array in C

What is an Array? The collection of a fixed number of elements of homogeneous data types that are stored in contiguous locations in the memory is known as an array. Only...

6 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

How to use Typedef Struct in C

The “typedef” is a predefined keyword in C programming language which is used to declare the new name to an existing type of variable. For better understanding, if you declare a...

3 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Random function in C

Random function in C In the C programming language, the rand() is a function used for Pseudo Random Number Generator (PRNG). The random number generated by the rand() function is not...

4 minutes read.

Displaying Array in C

Reference a collection of variables of similar data type in an array using a single element. The parts are stored next to each other. When declaring an array, you must specify...

4 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Bank Account System in C using File Handling

This tells about the creation of bank account system using the C language and handling of files in C. Approach Let us see the approaches and the functions how they are covering...

5 minutes read.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

1 minute read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

getch() function in C

getch() is a pre-define or built-in function present in the conio.h library. It returns the given character immediately without waiting for the enter key to be entered. By using getch()...

3 minutes read.