Write a program to find the total and average of a set of numbers entered by a
user. The following provides additional details on how the program should work:
• The program should prompt the user how many numbers they want o
process.
• Once user input is obtained, user will be asked to enter the numbers
used to calculate the total and average repeatedly. (Data validation
must be performed to ensure that the user input is between 1 to 100
inclusive)
• Finally, the total and average calculated will be displayed to the user.
using System;
class Program
{
static void Main(string[] args)
{
string str = "";
int Number = 0;
while (Number == 0)
{
Console.Write("Please enter an integer from 1 to 10:");
str = Console.ReadLine();
if (int.TryParse(str, out Number))
if (Number >= 1 && Number <= 10)
{
int[] nums = new int[Number];
int i = 0;
int value = 0;
while (i < Number)
{
Console.Write("Please enter an integer from 1 to 100:");
str = Console.ReadLine();
if (int.TryParse(str, out value))
if (value >= 1 && value <= 100)
{
nums[i] = value;
i++;
}
}
int average;
int total = 0;
for (i = 0; i < Number; i++)
total = total + nums[i];
average = total / Number;
Console.WriteLine("average: " + average);
Console.WriteLine("total: " + total);
}
else
Number = 0;
}
}
}
Comments
Leave a comment