ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to print only those numbers whose value is less than average of all elements in an integer array using LINQ

Here, we are going to learn how to print only those numbers whose value is less than average of all elements in an integer array using LINQ in C#?
Submitted by Nidhi, on August 21, 2020

Here we will create an array of Integers then we print only those employees whose value is less than average of all elements in an integer array using let statement of LINQ and print them on the console screen.

Program:

The source code to print numbers whose value is less than average of all elements of integer array using LINQ in C# is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

using System;
using System.Linq;

class Number
{
    static void Main()
    {
        int[] intArr = {123,456,789,012,345,567,890};
        
        var nums = from num in intArr
                   let total = intArr.Sum()
                   let avg   = total/intArr.Length
                   where num < avg
                   select num;
       
        Console.WriteLine("Numbers are :");
        foreach (int n in nums)
        {
            Console.Write("{0} ",n);
        }
        Console.WriteLine();
    }
}

Output:

Numbers are :
123 12 345
Press any key to continue . . .

Explanation:

In the above program, we created a class Number that contains the Main() method.

int[] intArr = {123,456,789,012,345,567,890};

In the Main() method we created an integer array that contains multiple numbers.

var nums = from num in intArr
    let total = intArr.Sum()
    let avg   = total/intArr.Length
    where num < avg
    select num;

In the above code, we select numbers whose value is less than average of all elements in integer array using let statement of LINQ.

Console.WriteLine("Numbers are :");
foreach (int n in nums)
{
    Console.Write("{0} ",n);
}
Console.WriteLine();

In the above code, we printed the numbers selected from the LINQ query on the console screen.

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.