public static class SpiralMatrix
public static void Main()
Console.Write("Въведи число: ");
int n = int.Parse(Console.ReadLine());
Direction direction = Direction.Right;
int[,] matrix = BuildMatrix(n, direction);
private static int[,] BuildMatrix(int n, Direction direction)
int maxRotations = n * n;
int[,] matrix = new int[n, n];
for (int i = 1; i <= maxRotations; i++) {
if (direction == Direction.Right && (col > n - 1 || matrix[row, col] != 0)) {
direction = Direction.Down;
if (direction == Direction.Down && (row > n - 1 || matrix[row, col] != 0)) {
direction = Direction.Left;
if (direction == Direction.Left && (col < 0 || matrix[row, col] != 0)) {
direction = Direction.UP;
if (direction == Direction.UP && row < 0 || matrix[row, col] != 0) {
direction = Direction.Right;
if (direction == Direction.Right) {
if (direction == Direction.Down) {
if (direction == Direction.Left) {
if (direction == Direction.UP) {
private static void renderMatrix (int n, int[,] matrix) {
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
Console.Write("{0,4}", matrix[r, c]);