How to print the N largest numbers in array with C#

1 Answer

0 votes
using System;

class Program
{
    public static void printNLargest(int[] arr, int N) {
        Array.Sort(arr);
        Array.Reverse(arr);
 
        for (int i = 0; i < N; i++)
            Console.Write(arr[i] + " ");
    }
    static void Main() {
        int[] arr = new int[] { 50, 99, 20, 100, 76, 33, 87, 40, 80, 85 };
        int N = 4;
        
        printNLargest(arr, N);
    }
}



/*
run:

100 99 87 85 

*/

 



answered May 15, 2022 by avibootz

Related questions

2 answers 145 views
1 answer 109 views
1 answer 111 views
1 answer 116 views
1 answer 121 views
1 answer 108 views
2 answers 155 views
...