The source code to find the average of array elements using the predefine method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
//C# program to find the average of array elements
//using the Average() method of Queryable class.
using System;
using System.Linq;
class Sample
{
static void Main()
{
int[] MyArray = { 40, 30, 20, 10, 50 };
double average = Queryable.Average(MyArray.AsQueryable());
Console.WriteLine("Average of array elements: {0}",average);
}
}
Output:
Average of array elements: 30
Press any key to continue . . .
Explanation:
In the above program, we created an array of integers. Then find the average of all array elements using the Average() method of Queryable class. The Average() method returns a double value. To use a Queryable class, we need to import the System.Linq method in our program.
Program:
The source code to find the average of array elements using the predefine method is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.
Output:
Explanation:
In the above program, we created an array of integers. Then find the average of all array elements using the Average() method of Queryable class. The Average() method returns a double value. To use a Queryable class, we need to import the System.Linq method in our program.