Home Programs How to find the Mean of a Set of Numbers in C#

How to find the Mean of a Set of Numbers in C#

In this example we show you how to get the mean of given Set of Numbers.

The mean is the average of the numbers. It is easy to calculate: add up all the numbers, then divide by how many numbers there are.

Example

In t his example we ask the user for 5 numbers

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1, number2, number3, number4, number5, avgNumber, sumNumber;
            Console.WriteLine("Enter 5 Numbers:");
            number1 = Convert.ToInt32(Console.ReadLine());
            number2 = Convert.ToInt32(Console.ReadLine());
            number3 = Convert.ToInt32(Console.ReadLine());
            number4 = Convert.ToInt32(Console.ReadLine());
            number5 = Convert.ToInt32(Console.ReadLine());
            sumNumber = (number1 + number2 + number3 + number4 + number5);
            avgNumber = (sumNumber / 5);
            Console.WriteLine("Sum is " + sumNumber);
            Console.WriteLine("Average is " + avgNumber);
            Console.ReadLine();
        }
    }
}

 

Here is a test run

Enter 5 Numbers:
10
20
30
40
50
Sum is 150
Average is 30

You may also like