Machine Problem 5.10.
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: E
Enter a list of numbers: 5 4 8 2 6
Enter a number to be searched: 2 2 found in location 4
Here is program:
static void Main(string[] args)
{
int[] arr = new int[5];
int f = 0;
int numsearch;
Console.WriteLine("Enter a list of numbers:");
for(int i = 0; i < arr.Length; i++)
{
arr[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine("Enter a number to be searched:");
numsearch = Int32.Parse(Console.ReadLine());
foreach(int j in arr)
{
if(j == numsearch)
{
Console.WriteLine(numsearch + " found in location " + f)
break;
}
f++;
}
}
Comments
Leave a comment