Write a program using one-dimensional array that searches a number if it is found on the list of the given 5 input numbers and locate its exact location in the list.
Sample input/output dialogue:
Enter a list of numbers: 5 4 8 2 6
Enter a number to be searched: 2 2 found in location 4
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
Console.Write("Enter a list of numbers: ");
string[] ints = Regex.Split(Console.ReadLine(), " ", RegexOptions.CultureInvariant);
int[] arr = new int[ints.Length];
for (int i = 0; i < ints.Length; ++i)
{
arr[i] = Convert.ToInt32(ints[i]);
}
Console.Write("Enter a number to be searched: ");
int num = Convert.ToInt32(Console.ReadLine());
int pos = Array.FindIndex(arr, (obj) => { return obj == num; }) + 1;
if (pos != 0)
{
Console.WriteLine($"{num} found in location {pos}");
}
else
{
Console.WriteLine($"{num} not found");
}
}
}
Comments
Leave a comment