C# program to check a specified number exists in an array using Linq

Here, we are going to learn how to check a specified number exists in an array using Linq in C#?
Submitted by Nidhi, on September 03, 2020

Here, we will create an array of float numbers and then check a specified number is exists in an array or not using Linq Contains() method.

Program:

The source code to check a specified number exists in the array using Linq, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to check a specified number exists 
//in an array using Linq.

using System;
using System.Linq;

class LinqDemo
{
    static void Main(string[] args)
    {
        float[] numbers = { 8.2F, 11.2F, 7.6F, 8.3F, 5.5F, 6.4F };

        bool isExist = false;

        isExist = numbers.Contains(12.5F);

        if(isExist==true)
            Console.WriteLine("Number is exist in the array");
        else
            Console.WriteLine("The number does not exist in the array");
    }
}

Output:

The number does not exist in the array
Press any key to continue . . .

Explanation:

In the above program, we created a class LinqDemo that contains Main() method, In the Main() method we created an array of a float number.

The Contains() method is used to check the specified number is exist in an array or not. Here, 12.5F number does not exist in the array then it will print "Number does not exist in the array" message on the console screen.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





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.