Saturday, 2 December 2017

C# Program to Perform Searching using Predefined Functions


Code:

using System;
class linSearch
{
    public static void Main()
    {
        Console.WriteLine("Enter Number of Elements you Want to Hold in the Array ? ");
        string s = Console.ReadLine();
        int x = Int32.Parse(s);
        int[] a = new int[x];
        Console.WriteLine("Enter Array Elements :");
        for (int i = 0; i < x; i++)
        {
            string s1 = Console.ReadLine();
            a[i] = Int32.Parse(s1);
        }
        Array.Sort(a);
        Console.WriteLine("Sorted Array : ");
        for (int i = 0; i < x; i++)
        {
            Console.WriteLine("{0}", a[i]);
        }
        Console.WriteLine("Enter the Element to be Searched : ");
        string s3 = Console.ReadLine();
        int x2 = Int32.Parse(s3);
        int x3 = Array.BinarySearch(a, (Object)x2);
        Console.WriteLine("BinarySearch: " + x3);
        Console.WriteLine("Element {0} is {1}", x3, a[x3]);
        Console.Read();
    }
}


Output:

Enter Number of Elements you Want to Hold in the Array ? 5
Enter Array Elements :
2
3
1
4
5
Sorted Array :
1
2
3
4
5
Enter the Element to be Searched : 4
Binary Search : 3
Element 3 is 4



More C# Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...