Q: Min, Max, Sum and Average of N Numbers - in C# (Task)

+4 votes

Write a program that reads from the console a sequence of n integer numbers and returns the minimal, the maximal number, the sum and the average of all numbers (displayed with 2 digits after the decimal point). The input starts by the number n (alone in a line) followed by n lines, each holding an integer number. The output is like in the examples below.


input n numbers in C# and use methods

asked in C# category by user eiorgert
edited by user golearnweb

1 Answer

+2 votes
Best answer

Very good share - as this is very important task to understand! Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

class MinMaxSumAndAverageOfNNumbers
	static void Main()
		int n = int.Parse(Console.ReadLine());
		int[] array = new int[n];

		for (int i = 0; i < n; i++)
			array[i] = int.Parse(Console.ReadLine());

		Console.WriteLine("min = {0}", array.Min());
		Console.WriteLine("max = {0}", array.Max());
		Console.WriteLine("sum = {0}", array.Sum());
		Console.WriteLine("avg = {0:F2}", array.Average());
answered by user nikole
selected by user golearnweb