In: Computer Science
write a C program that the user will enter how many subjects he has then the user will Enter grades for each subject he has after that the user will enter a grade for the subject out of 100 and the GBA will be counted for him the GBA will be out of 5 and If his GBA is 4.5 and above, print congratulations you got bonus + the GBA will Multiply to 20 and if it's above 80, print Congratulations, you have been accepted into the College of Computer Science and Engineering
The C program to take number of subjects(n) as input and to read scores of n subjects to calculate GBA and to print results accordingly is:
#include<stdio.h>
// input_scores() function takes score of a subject from user
// and checks if the score is vvalid. If then then score is returned
int input_scores(int i){
int sc;
printf("Subject %d score: ",i);
scanf("%d",&sc);
//Checking if score is less than 0 and greater than 100
//If yes then this function is called again to read input again
//It repeats till user enter valid input
if(sc<0 || sc>100){
printf("Invalid score!! Score can't be less than 0 and greater than 100\nTry again!\n");
input_scores(i);
}
else
{
return sc;
}
}
//Program execution starts here
int main(void){
//Declaring a variable numofsubs
int numofsubs;
printf("Enter number of subjects : ");
//Taking number of subjects as input from user
//and storing in numofsubs variable
scanf("%d",&numofsubs);
//Creating a integer array scores[] of size numofsubs
int scores[numofsubs];
//Declaring a floaating point varibale avg to store average of scores
float avg=0;
printf("Enter scores of each subject \n");
//Calling input_scores() function numofsubs times
//to take numofsubs scores from user
for(int i=0;i<numofsubs;i++){
scores[i]=input_scores(i+1);
//adding the read score to avg variable to calculate average at the end
avg = avg +scores[i];
}
//Calculating average
avg = avg/numofsubs;
//GBA is given by multiplying avg by 5 and dividing by 100
//This gives GBA out of 5
float gba = (5*avg)/100;
printf("Your GBA is: %f\n",gba);
//Checking for given conditions and printing respective outputs
if(gba>=4.5)
printf("Congratulations!!! You got bonus\n");
if(gba*20>80)
printf("Congratulations, you have been accepted into the College of Computer Science and Engineering");
else
{
printf("Sorry your GBA is low!!");
}
}
Code Snippet:
I have calculated GBA by first calculating the average of grades, then multiplying the average by 5 and dividing with 100 gives GBA out of 5.
Now I checked if GBA is greater than 4.5. If yes then Bonus is printed.
Then checked if GBA times 20 is greater than 80. If yes, then printed the message "Congratulations, you have been accepted into the College of Computer Science and Engineering".
I have provided comments in the program explaining lines of code. I have tested the code for all possible cases and it is doing good. This code also handles exceptions like scores less than 0 and greater than 100.
I am sharing few output screenshots for your reference.
Hope this answer helps you.
Thank you :)