Answer to Question #176706 in C# for Patrick Ian Kyle Mabunay

Question #176706

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-29T12:29:24-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Q176706
{
    class Program
    {
        static void Main(string[] args)
        {
            //get a list of numbers from the keyboard
            Console.Write("Enter a list of numbers: ");
            string[] tokens = Console.ReadLine().Split(' ');
            //using one-dimensional array
            int[] listNumbers = new int[tokens.Length];
            for (int i = 0; i < tokens.Length; i++) {
                listNumbers[i]=int.Parse(tokens[i]);
            }
            //get a number to be searched from the keyboard
            Console.Write("Enter a number to be searched: ");
            int target = int.Parse(Console.ReadLine());
            //search number
            for (int i = 0; i < listNumbers.Count(); i++)
            {
                if (listNumbers[i]== target)
                {
                    Console.WriteLine("{0} found in location {1}", target, i + 1);
                }
            }
            Console.ReadLine();
        }
    }
}


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