Answer to Question #176894 in C# for Patrick Mabunay

Question #176894

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



1
Expert's answer
2021-03-31T23:47:31-0400
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();
    }
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS