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:
Enter a list of numbers: 5 4 8 2 6
Enter a number to be searched: 2 2 found in location 4
using System;
namespace one_dim_mas
{
class Program
{
public static void Main(string[] args)
{
int i, n, search;
n = 5;
int [] array = new int[n];
Console.WriteLine("Enter a list of numbers:");
for (i=0; i<n; i++)
array[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a number to be searched: ");
search = Convert.ToInt32(Console.ReadLine());
Console.WriteLine();
for (i=0; i<n; i++)
if (search == array[i])
{
Console.WriteLine("{0} found in location {1}", search, i+1);
break;
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Comments
Leave a comment