## Simple C Program: Seneca GPA Calculator

This program was written by me in September 2010.

The idea of this program is very simple:
– Enter percentage either letter grades
– When you want to quit, you use “q”

Very simple, but also very useful program. All validation is made. (The source code is in the link below)
If you are having problems with compiling and running please see the post below, that instructs how to compile and run programs in different Operating Systems.

Please feel free to copy my source code, I have no problems with that.

Click here to see a screenshot of program in Terminal (Linux Ubuntu OS)

Here is a source code:

```/*
>>>>>>>>>>>How it works ?<<<<<<<<<<<<<<

GPA CALCULATIN is based on table from : http://www.senecac.on.ca/registrar/records/gpa.html
LETTER GRADE CONVERTER are based on table from : http://people.senecac.on.ca/lucie.dutfield/GPA.htm

GPA Calculator:
1.You enter number of subjects you need to calculate gpa for
2.You enter marks for your subjects. (Mark can include "+")
3.GPA claculator uses formula:
Grade Point Average = Total Grade Points Earned / Total Units of Credits.
4. If GPA is bigger than 3.55 you receive Honour or if more than 3.95 High Honour

You enter the percentage, script shows you letter grade

Created by Anatoly Spektor

Have fun! */

#include
#include
#define SIZE 3

/* Function prototypes */
void percent_to_letter(void);
void gpa_calc(void);
void clear_buffer(void);
int get_an_int(void);
float get_float(void);
int yesorno (void);
int validate_mark(char mark[]);

char choice = ‘o’;
printf("nt————————————-n")
printf("t SENECA COLLEGE GPA CALCULATOR");
printf("nt————————————–n");
printf("nt Please choose one of the following options:n");
printf("ntt[a] Convert percentage into letter grade");
printf("ntt[b] Calculate GPA");
printf("ntt[q] Quit Program");

/* Choose between GPA calculator or percentage to grade converter */

do {
printf("nnnt * Enter letter [a][b] or [q]: ");
choice=getchar();
clear_buffer();
choice=tolower(choice);
if ((choice != ‘a’) && (choice != ‘b’)&& (choice != ‘q’))
printf("nt >>> Only three options available: [a][b] and [q].n");
}while((choice != ‘a’) && (choice != ‘b’) && (choice != ‘q’));

printf("nt >>> Your option is [%c] nn", choice);

if (choice == ‘a’)
return 2;

if (choice == ‘b’)
return 1;
else if (choice == ‘q’){ /*[q] – Quit option */
printf("t*** Thank you for using GPA Calculator! Good bye! ***nn");
return 0;
EOF;

}
}

/* MAIN function – Switch between GPA and Percent to letter */
int main (void) {

int yn=0, numsub=0, option =-3;;

while (option != 0){

switch (option) {

case 1:
do{
gpa_calc(); /*Calling GPA */
printf("nt* Do you want to try again [yes] [no]: ");
yn=yesorno();
clear_buffer();
}while(yn != 0);
break;
case 2:
do{
percent_to_letter(); /* Calling Percent to letter converter */
printf("nt* Do you want to check another letter [yes] [no]: ");
yn = yesorno();
clear_buffer();
}while (yn != 0);

/* Quit option is inside display_menu */
}
}
return 0;

}

/* Percent to letter function */
void percent_to_letter () {

float num;

do {
num=get_float();

if (num >> Input can not contain negative numbersn");
else if (num > 100)
printf("nt>>> Max number is 100n");
}while((num  100));

if (num >> %.1f percent is letter : Fnn", num);
if ((num >=55)&&(num>> %.1f percent is letter : Dnn", num);
if ((num >=60)&&(num>> %.1f percent is letter : Cnn",num);
if ((num >=65)&&(num>> %.1f percent is letter : C+nn",num);
if ((num >=70)&&(num>> %.1f percent is letter : Bnn",num);
if ((num >=75)&&(num>> %.1f percent is letter : B+nn",num);
if ((num >=80)&&(num>> %.1f percent is letter : Ann",num);
if ((num >=90)&&(num>> %.1f percent is letter : A+nn",num);

}

void gpa_calc (void) {

int num =0;
char mark[SIZE]="OO";
float num_mark=0, u_of_credit=0, gpa_calc =0;

do{
printf("nt* Please enter number of subjects [max 10]: "); /* I assumed 10 subjects would be enough*/
num= get_an_int(); /* To avoid crashes calling function get_an_int instead of basic scanf */
if (num > 10)
printf("nt>>> Number of subjects should be 10 or less!n");
else if (num >> Number of subjects can not be negative!n");
}while ((num 10));

for (i=0; i >>>>>>>>>>>>>>>>>>>>n");

if (gpa_calc > 3.95)
printf("nntCongratulations, you will recieve HIGH HONOUR!nn");
else if(gpa_calc > 3.55)
printf("nntCongratulations, you will recieve HONOUR!nn");
printf("nnt This Program was created by Anatoly Spektorn");
printf("nt>>>>>>>>>>>>>>>>>>>>>>>>>n")  ;
}

void clear_buffer(void){ /* function needed to read characters carefully */
while(getchar() != ‘n’)
;
}

int get_an_int(void){
int n,error = 0;
char junk;

do{
if( 0 == scanf("%d", &n) || getchar() != ‘n’){
clear_buffer();
printf("nt>>> Error! Input should contain only numbersn");
printf("nt * Please try again. Enter number of subjects [max 10]: " );
error = 1;
} else error = 0;
}while( error );
return n;
}

float get_float(void){ /* needed to read floats carefully */
float n;
int error = 0;
char junk;

do{
if( 0 == scanf("%f", &n) || getchar() != ‘n’){
clear_buffer();
printf("nt>>> Error! Input should contain only numbersn");
printf("nt * Please try again. Enter number: " );
error = 1;
} else error = 0;
}while( error );
return n;
}

int yesorno() {
int c, rc = -1;
do {
c = getchar();
if ((c == ‘y’) || (c == ‘Y’)) {
c = getchar();
if ((c == ‘e’) || (c == ‘E’)) {
c = getchar();
if ((c == ‘s’) || (c == ‘S’)) {
rc = 1;
}
}
}
else if ((c == ‘n’) || (c == ‘N’)) {
c = getchar();
if ((c == ‘o’) || (c == ‘O’)) {
rc = 0;
}
}
if (rc == -1 && c != -1) {
printf("nt>>> There are only two options [yes] or [no]: ");
while (c != -1 && c != ‘n’)
c = getchar();
}
} while (rc == -1 && c != -1);
return rc;
}

int validate_mark ( char mark[]){

int i, val =0;

for (i=0; i>> Invalid Letter!n");
printf("nt >>> Available letters: A-F [including letters with ‘+’ sign])n");
return 0;
}
i++;
}
}```

Thanks for installing the Bottom of every post plugin by Corey Salzano. Contact me if you need custom WordPress plugins or website design. ### Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

## Discussion

1. ed

if my gpa formula is GPA = [ ∑( GPi X CHi ) ] ÷ [ ∑CHi ]
what should I put to change ∑ and calculate the gpa?

sorry. but I am very new to C.

2. ed

GPi is grade point obtained for a certain subject and CHi is Credit hour for a certain subject.

3. Skye Thompson

nice………….
I like it.

4. amimawe

why did you use #include ??
which syntax use that library?
does that library available in all c language software?

5. amimawe

i mean #include .

6. amimawe

7. Nishat Shammi
8. Nishat Shammi 