// Calculate average (only if the user inputted any numbers
if (count > 0)
average = sum / (double) count;
else
average = 0;
Create your own code to add to the original program, after Line 30. The code you add should display whether the total number of values entered by the user (count)=(4), is small
(< 5) or large (>= 5). Use an If/Else statement.
using System;
namespace App
{
class Program
{
static void Main(string[] args)
{
int value = 0, sum = 0, count = 0;
double average = 0.0;
// 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);
if (count >= 5)
{
Console.WriteLine("The total number of values entered by the user is large");
}
else {
Console.WriteLine("The total number of values entered by the user is small");
}
Console.ReadLine();
}
}
}
Comments
Leave a comment