In: Computer Science
Create a console app with C# that uses a while loop to calculate the average of 3 test scores.
Input integers, but use the appropriate average data type.
Generally, you use a while to do something while a condition is true. You can use a while loop to execute a certain number of times. *While* this is not the best use of the while loop and another loop is generally more appropriate, it follows the pattern below
set a counter to 0 or 1
set a sum variable to 0 set a total variable to 3 while the counter is less the total (or less than or equal to, depending on if you started at 0 or 1) {
write message
asking for input
read input
parse to a number
increment counter
}
Calculate average
print average with 2 decimal points precision*
using System;
class Program
{
static void Main() {
int count = 0, total = 0, number;
while (count < 3)
{
Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine());
total += number;
count++;
}
double average = total / 3.0;
Console.Write("Average = " + average.ToString("####0.00"));
}
}
// Hit the thumbs up if you are fine with the answer. Happy Learning!