In: Computer Science
Using Visual Studio write a program that allows the user to enter multiple scores ranging from 0 to 100, computes the average of
those scores and then writes out a letter grade using the typical grading scale. Your program will also:
1. Allow the user to enter any number of scores he or she wishes but assume there will be at least one
score entered.
2. Use a sentinel-controlled loop variable to terminate the loop when the user has no more scores to
enter. Hold user in a loop if a “yes” or “no” is not entered.
3. Use the TryParse method, to hold the user in a loop if a non-numeric value is entered. To exit the loop,
the user must enter correct input.
4. Hold the user in a loop if a value less than 0 or greater than 100 is entered.
using System;
class Program
{
static void Main() {
int n=0,check=1;;
double total=0,score=0,avg;
char grade='a';
string val;
Console.Write("Enter score (no to stop): ");
val = Console.ReadLine();
while (!val.Equals("no"))
{
check=1;
while (check==1)
{
check = 0;
if (!double.TryParse(val,out score))
{
Console.Write("Enter a numeric value!!!: ");
check=1;
val = Console.ReadLine();
}
else if (score<0 || score>100)
{
Console.Write("Score should be greater than 0 and less than 100: ");
val = Console.ReadLine();
check=1;
}
else
{
n=n+1;
total=score+total;
}
}
Console.Write("Enter score (no to stop): ");
val = Console.ReadLine();
}
avg=total/n;
if(avg>=80)
grade='A';
else if(avg>=60)
grade='B';
else if(avg>=50)
grade='C';
else if(avg>=40)
grade='D';
else
grade='F';
Console.WriteLine("Calculated grade: "+grade);
}
}