C# Program to Find the Average of a Sequence of Numeric Values



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
Updated on: 2020-06-23T07:40:42+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements