Answer to Question #187126 in C# for angelo r reyes

Question #187126

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 2found in location 4


1
Expert's answer
2021-04-29T06:53:27-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        static int[] getNumbers() {
            Console.Write("Enter a list of numbers: ");
            string[] values = Console.ReadLine().Split(' ');
            int[] listNumbers = new int[values.Length];
            for (int i = 0; i < values.Length; i++)
            {
                listNumbers[i] = int.Parse(values[i]);
            }
            return listNumbers;
        }


        static int getNumberSearched() {
            Console.Write("Enter a number to be searched: ");
            return int.Parse(Console.ReadLine());
        }
        static void Main(string[] args)
        {
            int[] listNumbers = getNumbers();
            int numberToSearch = getNumberSearched();


            for (int location = 0; location < listNumbers.Count(); location++)
            {
                if (listNumbers[location] == numberToSearch)
                {
                    Console.WriteLine("{0} found in location {1}", numberToSearch, location + 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
APPROVED BY CLIENTS