Machine Problem 5.3.
Write a program using one-‐dimensional array that accept five input values from the keyboard. Then it should also accept a number to search. This number is to be searched if it is among the five input values. If it is found, display the message “Searched number is found!”, otherwise display “Search number is lost!”.
Here is my program:
static void Main(string[] args)
{
int[] arr = new int[5];
int numtosearch;
Console.WriteLine("Enter values array: ");
for(int i = 0; i < arr.Length; i++)
{
arr[i] = Int32.Parse(Console.ReadLine());
}
Console.WriteLine("Enter number to search:");
numtosearch = Int32.Parse(Console.ReadLine());
foreach(int i in arr)
{
if (numtosearch == i)
{
Console.WriteLine("Searched number is found!");
break;
}
else
{
Console.WriteLine("Search number is lost!");
}
}
}
Comments
Leave a comment