using System; class Program { static void Main(string[] args) { while (true) { Console.Write("Enter number of rows/columns: "); int input; bool isNumber = int.TryParse(Console.ReadLine(), out input); if (!isNumber) { return; } int[,] array = new int[input, input]; int iterator = 0; int row = 0; int col = 0; int step = 1; for (int num = 1; num <= input * input; num++) { array[row, col] = num; switch (step) { case 1: if (col == (input - iterator - 1)) { row++; step++; } else { col++; } break; case 2: if (row == (input - iterator - 1)) { col--; step++; } else { row++; } break; case 3: if (col == (0 + iterator)) { row--; step++; } else { col--; } break; case 4: if (row == (1 + iterator)) { col++; step = 1; iterator++; } else { row--; } break; } } ShowGrid(array); } } public static void ShowGrid(int[,] array) { int rowLength = array.GetLength(0); int colLength = array.GetLength(1); Console.WriteLine(); for (int i = 0; i < rowLength; i++) { for (int j = 0; j < colLength; j++) { Console.Write(string.Format("{0}\t", array[i, j])); } Console.Write(Environment.NewLine + Environment.NewLine); } Console.WriteLine(); } }