find the largest number in csharp

To find the largest number in C#, you can use conditional statements to compare a set of numbers and identify the largest one. Here's an example code snippet that shows how to do this:

main.cs
int[] numbers = { 10, 20, 30, 40, 50 };
int maxNumber = numbers[0];

for (int i = 1; i < numbers.Length; i++)
{
    if (numbers[i] > maxNumber)
    {
        maxNumber = numbers[i];
    }
}

Console.WriteLine("The largest number is: " + maxNumber);
249 chars
13 lines

In this code, we have an array of integers that contains a set of numbers. We initialize a variable maxNumber with the first number in the array. Then, we use a for loop to look at each number in the array and compare it to maxNumber. If we find a number that is greater than maxNumber, we update the value of maxNumber to be that number. At the end of the loop, maxNumber will contain the largest number in the array. Finally, we print out the value of maxNumber using Console.WriteLine().

gistlibby LogSnag