C# - Find the largest number in an array
C# - Find the largest number in an array. Loop through the elements in the array and print the largest number.
CODE
using System;
class Program
{
public static void Main()
{
int[] numbers = new int[] { 10, 999, -76, 45, 90, 23, 23, 2, 3, 4, 5 };
if (numbers.Length < 1)
Console.WriteLine("The Array should contain atleast 1 element.");
else
{
//The below steps will be reached only if the Array Length is > 1
int largestNumber = numbers[0];
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] > largestNumber)
largestNumber = numbers[i];
}
Console.WriteLine("The largest number in the given array is: {0}", largestNumber);
}
Console.ReadKey();
}
}
Comments
Post a Comment