Question: using System; public static class Lab 4 _ 1 { public static void Main ( ) { int value = 0 , sum = 0

using System;
public static class Lab4_1
{
public static void Main()
{
int value =0, sum =0, count =0;
double average =0.0;
Console.Clear();
// Read initial value (seed the loop)
Console.Write("Enter a positive integer (-1 to stop): ");
value = Convert.ToInt32(Console.ReadLine());
// if the inputted value is not the sentinel value, process it
while (value >=0)
{
// Calculate the running total
sum += value; // same as sum = sum + value;
// Keep track of the number of inputted values
count++; // same as count = count +1;
// Read next value
Console.Write("Enter a positive integer (-1 to stop): ");
value = Convert.ToInt32(Console.ReadLine());
}
// Calculate average (only if the user inputted any numbers)
if (count >0)
average = sum /(double) count;
else
average =0;
// Print results
Console.WriteLine("sum ={0}, count ={1}", sum, count);
Console.WriteLine("average ={0:F2}", average);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!