The program finds find top two maximum numbers in the
given array. Finding process works without using any sorting functions and any kind of collections in C#.
C# Program to Find Max First Two Value in Array
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 32 33 34 |
class Program { static void Main(string[] args) { //given array int[] numbers = { 55, 4, 28, 27, 35, 1, 109, 3 }; //finding max top two numbers in given array int maxFirst = 0; int maxSecond = 0; foreach (int n in numbers) { if (maxFirst < n) { maxSecond = maxFirst; maxFirst = n; } else if (maxSecond < n) { maxSecond = n; } } Console.WriteLine("First Max Number: " + maxFirst); Console.WriteLine("Second Max Number: " + maxSecond); Console.ReadLine(); } } |