C# - Linear Search Implementation
C# - Linear Search Implementation
CODE
using System;
class Program
{
public static void Main(string[] args)
{
int[] numbers = new int[] { 1, 10, 99, 89, 35, 96, 6, 7, 100, 120, 55 };
int numberToSearch = 99;
bool isMatchFound = false;
for (int i = 0; i < numbers.Length; i++)
{
//Set the flag and exit the loop, if a match is found
if (numbers[i] == numberToSearch) {
isMatchFound = true;
break;
}
}
if (isMatchFound)
Console.WriteLine("The number {0} is present in the array.", numberToSearch);
else
Console.WriteLine("The number {0} is not present in the array.", numberToSearch);
Console.ReadKey();
//OUTPUT
//The number 99 is present in the array.
}
}
Comments
Post a Comment