Write a program that first requests from the user how many integers will be entered. It then reads
in those integers and calculates and displays the average of the numbers (to the nearest 2
decimal points). It must cater for a scenario where the user enters 0 numbers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace C_SHARP
{
class Program
{
static void Main(string[] args)
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-us");
// program that first requests from the user how many integers will be entered.
Console.Write("How many integers will be entered?: ");
int n = 0;
int.TryParse(Console.ReadLine(), out n);
if (n > 0)
{
// It then reads in those integers and calculates and displays the average of the numbers (to the nearest 2 decimal points).
double sum = 0;
for (int i = 0; i < n; i++)
{
Console.Write("Enter integer: ");
int integerValue = 0;
int.TryParse(Console.ReadLine(), out integerValue);
sum += integerValue;
}
double average=sum/n;
Console.WriteLine("Average = {0}", average.ToString("N"));
}
else {
Console.Write("Enter n>0.");
}
Console.ReadLine();
}
}
}
Comments
Leave a comment