C# Program to search an element in the array
In this program, You will learn how to search an element in the array in C#.
Array List is: 10 20 30 40 50 60 70 80 90 100
Enter number for search: 20
An element found: 20
Example: How to search an element in an array in C#.
using System;
public class Program
{
public static void Main(string[] args)
{
int i, n, f = 0;
int[] arr = new int[5];
Console.Write("Enter five numbers:");
for (i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Enter a number for search:");
n = Convert.ToInt32(Console.ReadLine());
for (i = 0; i < 5; i++)
{
if (arr[i] == n)
{
f = 1;
}
}
if (f == 1)
{
Console.WriteLine("Number found:" + n);
}
else
{
Console.WriteLine("Number not found:" + n);
}
}
}
Output:
Enter five numbers:10
20
30
40
50
Enter a number for search:30
Number found:30