Sort Data - C# Programming Exercise

This Exercise in C# involves creating a program that asks the user for 10 integer numbers (from -1000 to 1000), sorts them, and then displays them in ascending order. The goal of this Exercise is to practice using arrays, user input, and sorting numbers in C#. Additionally, it will reinforce the use of sorting methods such as Array.Sort(), which simplifies the process of ordering data in a collection in C#.

In this Exercise, the program will ask the user to input 10 integers within the specified range, store them in an array, and then use the Array.Sort() method to sort the numbers in ascending order. Finally, the program will display the sorted list of numbers.

This type of Exercise is a great way to practice basic skills in C#, such as working with arrays, sorting data, and handling user input. It provides a good opportunity to enhance your knowledge of how to work with simple data structures and apply common programming techniques.

 Category

Arrays, Structures and Strings

 Exercise

Sort Data

 Objective

Write a C# program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted.

 Write Your C# Exercise

using System;  // Importing the System namespace to access basic classes like Console

class Program
{
    static void Main()
    {
        int[] numbers = new int[10];  // Declare an array to store 10 integer numbers

        // Ask the user for 10 integer numbers within the range of -1000 to 1000
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.Write($"Enter number {i + 1} (from -1000 to 1000): ");
            numbers[i] = int.Parse(Console.ReadLine());  // Store each entered number in the array
        }

        // Sort the array of numbers in ascending order
        Array.Sort(numbers);  // Use the Array.Sort method to sort the numbers

        // Display the sorted numbers
        Console.WriteLine("\nSorted numbers:");
        foreach (int number in numbers)
        {
            Console.WriteLine(number);  // Print each number in the sorted array
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Two dimensional array as buffer for screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" 80 letters (e.g., 'X') in random positions withi...

  •  Two dimensional array 2: circunference on screen

    This Exercise in C# involves creating a program that declares a 70x20 two-dimensional array of characters and "draws" a circumference with a radius of 8 inside it. To...

  •  Computer programs

    Exercise in C# involves creating a program that can store up to 1000 records of computer programs. For each program, the following data should be stored: name,...

  •  Exercise tasks

    This exercise in C# consists of creating a program that can store up to 2000 "to-do tasks". Each task must store the following data: • Date (a set of 3 data: ...

  •  Household accounts

    This exercise in C# consists of creating a program that can store up to 10,000 costs and revenues, to create a small domestic accounting system. For eac...

  •  Reverse array

    In this C# exercise, you are asked to write a program that prompts the user for 5 numbers, stores them in an array, and displays them in reverse order. ...