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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Q_176894
{
class Program
{
static void Main(string[] args)
{
int n = 5; //The number of specified input numbers
bool isFind = false; //Boolean variable to search for an element in an array
int a = 0; //Array element to search
int[] array = new int[n]; //Create an array with 5 elements
int i = 0; //The ordinal number of the element
while (i < n)
{
Console.WriteLine("Enter array element: ");
/* Entering array elements from the keyboard
* and fill the array with them */
array[i] = Int32.Parse(Console.ReadLine());
Console.WriteLine();
i++;
}
Console.WriteLine("Enter an element to search: ");
a = Int32.Parse(Console.ReadLine());
for (i = 0; i < n; i = i + 1)
Console.WriteLine("Element[" + i + "]: " + array[i]);
for (i = 0; i < n; i = i + 1)
{
if( array[i]==a)
{
isFind = true;
Console.WriteLine($"{array[i]} found in cell {i+1}");
}
}
if (!isFind)
{
Console.WriteLine("Number not found!");
}
Console.ReadKey();
}
}
}
Comments
Leave a comment