Task : You want to calculate a student\'s GPA for a number of classes taken by t
ID: 3814563 • Letter: T
Question
Task: You want to calculate a student's GPA for a number of classes taken by the student during a single semester. Code should be in C#.
Inputs: 1. the student's name
2. Class names for the classes taken by the student
3. Class letter grade for the classes taken by the student
4. Class credit hours for the classes taken by the student
Processing: 1. Accept and process classes until the user indicates they are finished
2. accumulate the number of credit hours taken by the student
3. Calculate the total number of "points" earned by the student as:
(a) For each class calculate the points by multiplying the credit hours for that class times the numeric equivalent of the letter grade. ( A=4.0 B=3.0 C=2.0 D=1.0 F=0)
(b) Total all points for all classes
4. Calculate the GPA as the total number of "points" divided by the total credit hours.
Output: Display a nicely worded message that includes the student's name and the GPA (decimal point number with 2 decimal places) achieved by the student that semester.
Explanation / Answer
using System;
public class Test
{
public static void Main()
{
Console.WriteLine("Enter student name");
string name = Console.ReadLine();
double grade = 0;
double totalCreditHours = 0;
double points = 0;
string className =" ";
char letterGrade = 'G';
do
{
Console.WriteLine("Enter the class name, letter grade for the class and credit hours <enter stop to end>");
className = Console.ReadLine();
if(className == "stop")//exit the do while loop if stop is entered
break;
letterGrade = Convert.ToChar(Console.ReadLine());
double creditHours = Convert.ToDouble(Console.ReadLine());
switch(letterGrade)
{
case 'A' : grade = 4.0;
break;
case 'B' : grade = 3.0;
break;
case 'C' : grade = 2.0;
break;
case 'D' : grade = 1.0;
break;
case 'F' : grade = 0.0;
break;
default : Console.WriteLine("Invalid Grade");
break;
}
points = points + creditHours*grade;
totalCreditHours = totalCreditHours + creditHours;
}while(className != "stop");
decimal GPA = Convert.ToDecimal(string.Format("{0:0.00}", points/totalCreditHours));//format GPA tp 2 decimal places
Console.WriteLine(name + " has got "+ GPA +" GPA .");
}
}
Output:
Enter student name Nancy Williams
Enter the class name, letter grade for the class and credit hours <enter stop to end>
Computer Networks
B
40
Enter the class name, letter grade for the class and credit hours <enter stop to end>
C#
B
35
Enter the class name, letter grade for the class and credit hours <enter stop to end>
Maths
A
25
Enter the class name, letter grade for the class and credit hours <enter stop to end> stop
Nancy Williams has got 3.25 GPA .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.