- Course
- Arrays
- Search an array of integers
Search an array of integers
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a program in C# that serves to search an array of integer values. To do this, follow these steps:
- Get the quantity (x) of values from the array of integers
- Prepare an array of integers with the previous amount
- Get the number (and) to look for in the array of integers
- Show whether the number (y) exists in the array of integers or not
- Repeat until the end text is entered
Input
3
2
5
3
5
end
Output
The number 5 exist
Solution
using System;
public class SearchArrayIntegers
{
public static void Main(string[] args)
{
int x = Convert.ToInt32(Console.ReadLine());
int[] list = new int[x];
for (int i = 0; i < x; i++)
{
list[i] = Convert.ToInt32(Console.ReadLine());
}
string y;
do
{
y = Console.ReadLine();
if (y != "end")
{
int yToInt = Convert.ToInt32(y);
if (Array.BinarySearch(list, yToInt) >= 0)
{
Console.WriteLine("The number {0} exist", y);
}
else
{
Console.WriteLine("The number {0} not exist", y);
}
}
}
while (y != "end");
}
}