C# Average Method

CsharpProgrammingServer Side Programming

To find the average of integers in C#, use the Queryable Average() method.

Let’s say the following is our integer array.

var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };

Now, use the Average() method to get the average of the elements.

double avg = Queryable.Average(arr.AsQueryable());

Example

 Live Demo

using System;
using System.Linq;
class Demo {
   static void Main() {
      var arr = new int[] { 10, 17, 25, 30, 40, 55, 60, 70 };
      double avg = Queryable.Average(arr.AsQueryable());
      Console.WriteLine("Average = "+avg);
   }
}

Output

Average = 38.375
raja
Published on 14-Sep-2018 16:31:41
Advertisements