In C# you can find maximum or minimum value in a numeric array by looping through the array.
Here is the code to do that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Program { static void Main(string[] args) { //www.csharp-console-examples.com int[] numbers = new int[10]; Random rnd = new Random(); int min, max; for (int i = 0; i < numbers.Length; i++) { numbers[i] = rnd.Next(1, 100); Console.WriteLine(numbers[i]); } min = numbers[0]; max = numbers[0]; for (int i = 1; i < numbers.Length; i++) { if (min > numbers[i]) min = numbers[i]; if (max < numbers[i]) max = numbers[i]; } Console.WriteLine("====================================="); Console.WriteLine("The highest number in the array: > > > " + max); Console.WriteLine("The lowest number in the array: > > > " + min); Console.ReadKey(); } } |
Output:
C# program using for loop to find maximum value from an array