My task is to let a user input numbers, and when they input 0, the loop will end and should display the lowest number. However since they're inputting 0, that becomes the lowest number. I have tried using the OrderBy to skip the first number in the array, but that doesn't seem to be working.
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Task 7\n");
int[] numbers = new int[100];
for(int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("Input any number. Once you enter 0, the application will end");
numbers[i] = int.Parse(Console.ReadLine());
if(numbers[i] == 0)
{
break;
}
}
int lowest = numbers.OrderBy(num => num).Skip(1).First();
Console.WriteLine($"The lowest number was: {lowest}");
Console.ReadLine();
}
}
Maybe there's a smarter way overall to end the loop when the user inputs 0
Any ideas?
0into the array. But then, in anintarray every element is initialized with0. So unless the user enters all 100 (or whatever) numbers, there's gonna be0s in it. But you could use aListinstead of an array.0. Use another variable to count number of items user have entered (before he enters 0, which is not a problem at all to store) or as per @stickybit comment, use aList<int>.