Answer to Question #190318 in C# for Nkhululeko Chabala

Question #190318

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.


1
Expert's answer
2021-05-08T10:58:28-0400
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();
        }
    }
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS