In: Computer Science
C# CODE C#
1) Write a program that calculates a student's GPA. Remember, an A is worth 4 points, a B is worth 3 points, a C is worth 2 points, a D is worth 1 point, and an F is worth 0 points. For the purposes of this assignment, assume that all classes carry the same number of credit hours.
2) Use a sentinel-controlled loop to gather a variable number of letter grades. This should terminate the loop if the user enters an "X." This means that users should input as many grades as they like. When they are finished, they need to input an X, and at that point, the program will return the results.
C# CODE C# THANK YOU!
Assumption, here is the same number of credit hours here is 2.
The formula of GPA is as below
The C# code with sample output is as below.
using System;
class GPACalculator
{
static void Main()
{
char input;
int sum= 0,points=0;
int credit = 2,sumCredit=0;
//sentinel controlled loop
while (true)
{
Console.Write("Enter a grade- A,B,C,D or F (X to quit) : ");
//character input
input = Console.ReadLine()[0];
//break the loop only when input is X///
if (input == 'X')
{
break;
}
//assign points depending on grades using switch statement
switch(input){
case 'A':
points = 4;
break;
case 'B':
points = 3;
break;
case 'C':
points = 2;
break;
case 'D':
points = 1;
break;
case 'F':
points = 0;
break;
}
//find total credits in sumCredit and total points earned in
points*credit
sum += (points*credit);
sumCredit +=credit;
}
//find the gpa and display
Console.WriteLine($"GPA of the student is = {sum /
(double)sumCredit}");
}
}
sample output