Basic Programming C# Examples

C# Program to Sort elements of Array in Ascending order

The Program in C# Program to Sort elements of Array in Ascending order is given below:

using System;

namespace ArraySortAscending
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Codeauri Family, enter the number of elements in the array to sort in ascending 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);

            Console.WriteLine("Okay, the Sorted array in ascending 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 ascending order:
3

Enter the elements of the array:
908
678
8
Okay, the Sorted array in ascending order:
8 678 908

Pro-Tips💡

Here are the steps by steps execution of above program:

  1. The program starts with the Main method that runs when the program is executed.
  2. The program first prompts the user to enter the number of elements in the array using the Console.WriteLine() method.
  3. The user inputs the number of elements and the program stores it in the variable n using the int.Parse() method.
  4. The program declares an integer array arr with n elements.
  5. The program then prompts the user to enter the elements of the array.
  6. The user inputs the elements of the array and the program stores them in the arr array.
  7. The program then sorts the elements of the array in ascending order using the Array.Sort() method.
  8. The program then displays the sorted array elements using a foreach loop that goes through each element of the array and outputs it using the Console.Write() method.
  9. Finally, the program ends by using the Console.WriteLine() method to create a new line in the console.

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

Programming Languages( Types, Pros and Cons)-Codeauri

What are the types or levels of Programming Languages? The types or levels of programming languages are divided into two types: Types Of Programming Languages: 1.Machine-level Language :1st…

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X