Use of “while loop” :

class Program
{
  static void Main(string[] args)
  {
    int total=0;
    int gradeCounter=0;
    int gradeValue;
    double average;
 
    Console.Write(“Enter -1 to quit :”);
    gradeValue = Int32.Parse(Console.ReadLine());  // Take user input in gradeValue.
 
    while (gradeValue!=-1)         //When gradeValue == -1 then while loop will close.
    {
      total = total + gradeValue;
      gradeCounter = gradeCounter + 1;     //To know the student count Add 1.
      Console.Write(“Enter -1 to quit :”);
      gradeValue = Int32.Parse(Console.ReadLine());
    }
            
    if(gradeCounter!=0)
    {
      average = (double) total/gradeCounter;
      Console.WriteLine(“nAverage number : {0} nTotal count:{1}” ,average,gradeCounter);
      Console.ReadLine();
    }
     
    else
    {
      Console.WriteLine(“nNo grade were entered.”);
      Console.ReadLine();
     }
  } 
}

Leave a Reply