In: Computer Science
Write application in C# that enables a user to:
Use Methods for user input and calculations
input the grade and number of credit hours for any number of courses.
Calculate the GPA on a 4.0 scale using those values. Grade point average (GPA) is calculated by dividing the total amount of grade points earned, sometimes referred to as quality points, by the total number of credit hours attempted. For each hour, an A receives 4 grade or quality points, a B receives 3 points, a C receives 2 points, and a D receives 1 point. Thus, a three-credit hour course receiving an A would have 12 quality points associated with the course.
Allow the user to input any number of courses and associated grades.
Display the number of hours earned and the calculated GPA.
Code for the given problem:
For each course,
using System;
public class Test
{
public static void Main()
{
int n;
int total_grades = 0;
int max_grades = 0;
int total_hours = 0;
n = int.Parse(Console.ReadLine());
for (int i=0; i<n; i++) {
string[] tokens = Console.ReadLine().Split();
string grade = tokens[0];
int g;
if(grade=="A")
g=4;
else if(grade=="B")
g=3;
else if(grade=="C")
g=2;
else
g=1;
int h = int.Parse(tokens[1]);
total_grades += g*h;
total_hours += h;
max_grades += 4*h;
}
float gpa = 0.0f;
gpa = ((float)total_grades / max_grades)*4;
Console.WriteLine("Number of hours earned = {0}, GPA = {1}", total_hours, gpa);
}
}
Input format:
Output format: