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 158 views
1 answer 119 views
1 answer 125 views
1 answer 127 views
1 answer 129 views
1 answer 119 views
2 answers 165 views
...