C# Program to Print Only Those Numbers Whose Value is Less Than Average of all Elements in an Integer Array using LINQ
Language-Integrated Query (LINQ) is a uniform query syntax in C# to retrieve data from different sources. It eliminates the mismatch between programming languages and databases and also provides a single querying interface for different types of data sources. In this article, we will learn how to print only those numbers whose value is less than the average of all elements in an integer array using LINQ in C#.
Input: 464, 23, 123, 456, 765, 345, 896, 13, 4 Output: Average is 343 So the numbers less than the average are: 23 123 13 4 Input: 264, 3, 223, 556, 1, 965, 145, 2, 14 Output: Average is 241 So the numbers less than the average are: 3 223 1 145 2 14
To print only those numbers whose value is less than average of all elements in an array we use the following approach:
- Store integer(input) in an array.
- The sum of the elements is calculated using the Sum() method.
- The average of the array is calculated by dividing the sum by the length of the array.
- By using the LINQ query we will store the numbers less than the average of the array in an iterator.
- Now the iterator is iterated and the integers are printed.
Average is 343 The Numbers: 23 123 13 4