Programming Course in C# ¡Free!

Sort data

 Saturday, April 06, 2013 published by Exercises C#
Proposed exercise


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

Output



Solution


using System;
public class Sortnumbers
{
public static void Main()
{
int[] numbers = new int[9];
int i = 0;

for (; i < total;i++)
{
Console.Write("Number {0}: ", i + 1);
numbers[i] = Convert.ToInt32(Console.ReadLine());
}   


for(i = 0; i < total - 1; i++)
{
for(int j = i + 1; j < total; j++)
{
if (numbers[i] > numbers[j])
{
int aux = numbers[i];

numbers[i] = numbers[j];
numbers[j] = aux;
}
}
}

Console.Write("Sorted:");

foreach (int number in numbers) 
Console.Write("{0} ", number);
}
}