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
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