In: Computer Science
Write application that enables a user to 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. Must be in C#.
In C sharp
using System;
namespace BoxApplication
{
class GPA
{
static void Main(string[] args)
{
double CH, num, c1 = 0.0, c2 = 0.0;
char grade;
// ask the user for the number of subjects
Console.WriteLine("\nEnter the number of subjects: ");
num = Convert.ToInt32(Console.ReadLine());
/* ask quality points and credit hours for each of the
subjects and calculate the total quality points and
the total credit hours */
for (int i = 0; i < num; i++)
{
Console.WriteLine("\nEnter the grade (A / B / C / D): ");
grade = Console.ReadKey().KeyChar;
Console.WriteLine("\nEnter the credit hours:");
CH = Convert.ToInt32(Console.ReadLine());
c1 = c1 + CH;
if (grade == 'A')
{ c2 = c2 + 4 * CH; }
if (grade == 'B')
{ c2 = c2 + 3 * CH; }
if (grade == 'C')
{ c2 = c2 + 2 * CH; }
if (grade == 'D')
{ c2 = c2 + 1 * CH; }
}
/*calculate GPA by dividing the total quality
* points by total credit hours and print the result
*/
Console.WriteLine("\n\n-------------------------------------------------\n");
Console.WriteLine("-------------------------------------------------\n");
Console.WriteLine("Total grades earned by the student is :");
Console.WriteLine(c2);
Console.WriteLine("\nTotal credit hours earned by the student is :");
Console.WriteLine(c1);
Console.WriteLine("\nGPA of the student is :");
Console.WriteLine(c2 / c1);
Console.ReadKey();
Console.ReadKey();
}
}
}
Code screenshot:
Output: