Objective
Write a C# program that says if a data belongs in a list that was previously created.
The steps to take are:
- Ask the user how many data will he enter.
- Reserve space for that amount of numbers (floating point).
- Request the data to the user
- Later, repeat:
* Ask the user for a number (execution ends when he enters "end" instead of a number).
* Say if that number is listed or not.
Must be done in pairs. but you must provide a single source file, containing the names of both programmers in a comment.
Write Your C# Exercise
C# Exercise Example
using System; // Import the System namespace for basic functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Comment with the names of both programmers
// Programmers: [Your Name] & [Partner's Name]
// Ask the user how many data points they want to enter
Console.Write("How many numbers will you enter? ");
int count = Convert.ToInt32(Console.ReadLine()); // Read and store the number of data points
// Create an array to store the floating point numbers
double[] numbers = new double[count];
// Ask the user for the data and store it in the array
Console.WriteLine("Enter the numbers:");
for (int i = 0; i < count; i++)
{
Console.Write($"Enter number {i + 1}: "); // Prompt the user for a number
numbers[i] = Convert.ToDouble(Console.ReadLine()); // Convert the input to a double and store it in the array
}
// Start checking if the entered numbers exist in the list
while (true)
{
Console.Write("\nEnter a number to search for (or 'end' to quit): ");
string input = Console.ReadLine(); // Read user input
// Check if the user wants to end the program
if (input.ToLower() == "end")
{
break; // Exit the loop if the user types "end"
}
// Convert the input to a double and check if it exists in the array
double searchValue;
bool isValid = double.TryParse(input, out searchValue); // Check if the input can be parsed as a double
if (isValid)
{
// Check if the number exists in the array
bool found = false;
foreach (double number in numbers)
{
if (number == searchValue)
{
found = true;
break;
}
}
// Display whether the number is found in the array or not
if (found)
{
Console.WriteLine($"The number {searchValue} is in the list.");
}
else
{
Console.WriteLine($"The number {searchValue} is not in the list.");
}
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number or 'end' to quit.");
}
}
Console.WriteLine("Goodbye!");
}
}