C# Average Method


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

Updated on: 23-Jun-2020

16K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements