The Program in C# Program to Sort Elements Of Array in Descending order is given below:
using System;
namespace ArraySortDescending
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array to sort in descending order: ");
int n = int.Parse(Console.ReadLine());
int[] arr = new int[n];
Console.WriteLine("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
arr[i] = int.Parse(Console.ReadLine());
}
Array.Sort(arr);
Array.Reverse(arr);
Console.WriteLine("Okay, the Sorted array in descending order: ");
foreach (int item in arr)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
}
Output:
Hello Codeauri Family, enter the number of elements in the array to sort in descending order:
3
Enter the elements of the array:
145
4
199
Okay, the Sorted array in descending order:
199 145 4
Pro-Tips💡
Here are the steps by steps execution of above program:
- Displays a message asking the user to enter the number of elements in the array to be sorted.
- Reads the number of elements in the array from the user.
- Creates an array with the specified number of elements.
- Displays a message asking the user to enter the elements of the array.
- Reads the elements of the array from the user.
- Sorts the array in ascending order using the
Array.Sort
method. - Reverses the order of the elements in the array using the
Array.Reverse
method. - Displays a message indicating that the array has been sorted in descending order.
- Writes the sorted array elements to the console.
- Exits the program.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.