Using C#, Visual Studio, Write a program that calculates a student\'s GPA. Remem
ID: 3868491 • Letter: U
Question
Using C#, Visual Studio, 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. 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.
Explanation / Answer
using System;
public class Test
{
public static void Main()
{
string grade;
double gpa = 0;
int n = 0;
Console.WriteLine("Enter the grade : ");
grade = Console.ReadLine();
while(grade != "X") // sentinel controlled while loop
{
switch(grade)
{
case "A": gpa = gpa + 4;
n++;
break;
case "B": gpa = gpa + 3;
n++;
break;
case "C": gpa = gpa + 2;
n++;
break;
case "D": gpa = gpa + 1;
n++;
break;
case "F": gpa = gpa + 0;
n++;
break;
default : Console.WriteLine(" Invalid grade");
break;
}
Console.WriteLine("Enter the grade : ");
grade = Console.ReadLine();
if(grade == "X") //if grade = x, exit the loop
break;
else
n++;
}
Console.WriteLine(" Student GPA : "+ gpa/n);
}
}
Output:
Enter the grade : A
Enter the grade : B
Enter the grade : C
Enter the grade : D
Enter the grade : F
Enter the grade : A
Enter the grade : E
Invalid grade
Enter the grade : X
Student GPA : 1.16666666666667
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.