C# Program to find the average of a sequence of numeric values

CsharpProgrammingServer Side Programming

Use the Linq Average() method to find the average of a sequence of numeric values.

Firstly, set a sequence.

List<int> list = new List<int> { 5, 8, 13, 35, 67 };

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

Queryable.Average(list.AsQueryable());

Example

 Live Demo

using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
   static void Main() {
      List<int> list = new List<int> { 5, 8, 13, 35, 67 };
      double avg = Queryable.Average(list.AsQueryable());
      Console.WriteLine("Average = "+avg);
   }
}

Output

Average = 25.6
raja
Published on 12-Sep-2018 09:33:58
Advertisements