Spiral Matrix Number Pattern in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2D Arrays

What You’ll Learn

How to fill a square matrix in a clockwise spiral using boundary pointers. This is a classic exercise for mastering 2D arrays and loop control.

⭐ Pattern Output

For a 10×10 matrix, the spiral looks like this:

Output
1    2    3    4    5    6    7    8    9   10
36  37   38   39   40   41   42   43   44   11
35  64   65   66   67   68   69   70   45   12
34  63   84   85   86   87   88   71   46   13
33  62   83   96   97   98   89   72   47   14
32  61   82   95  100   99   90   73   48   15
31  60   81   94   93   92   91   74   49   16
30  59   80   79   78   77   76   75   50   17
29  58   57   56   55   54   53   52   51   18
28  27   26   25   24   23   22   21   20   19
1

Complete C Program (Fixed Size)

This version uses #define SIZE 10 and fills numbers from 1 to 100 in a spiral.

c
#include <stdio.h>

#define SIZE 10

int main() {
    int matrix[SIZE][SIZE];
    int i, j, num = 1;
    int row_start = 0, row_end = SIZE - 1;
    int col_start = 0, col_end = SIZE - 1;

    while (num <= SIZE * SIZE) {
        for (i = col_start; i <= col_end; i++)
            matrix[row_start][i] = num++;
        row_start++;

        for (i = row_start; i <= row_end; i++)
            matrix[i][col_end] = num++;
        col_end--;

        for (i = col_end; i >= col_start; i--)
            matrix[row_end][i] = num++;
        row_end--;

        for (i = row_end; i >= row_start; i--)
            matrix[i][col_start] = num++;
        col_start++;
    }

    for (i = 0; i < SIZE; i++) {
        for (j = 0; j < SIZE; j++)
            printf("%4d", matrix[i][j]);
        printf("\n");
    }

    return 0;
}
2

Variation — User Input Size

This version reads the size and fills a variable-length matrix.

c
#include <stdio.h>

int main() {
    int size;
    int i, j, num = 1;
    int row_start, row_end, col_start, col_end;

    printf("Enter the size of the matrix: ");
    scanf("%d", &size);

    int matrix[size][size];
    row_start = 0;
    row_end = size - 1;
    col_start = 0;
    col_end = size - 1;

    while (num <= size * size) {
        for (i = col_start; i <= col_end; i++)
            matrix[row_start][i] = num++;
        row_start++;

        for (i = row_start; i <= row_end; i++)
            matrix[i][col_end] = num++;
        col_end--;

        for (i = col_end; i >= col_start; i--)
            matrix[row_end][i] = num++;
        row_end--;

        for (i = row_end; i >= row_start; i--)
            matrix[i][col_start] = num++;
        col_start++;
    }

    for (i = 0; i < size; i++) {
        for (j = 0; j < size; j++)
            printf("%4d", matrix[i][j]);
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Swap loop order to create a counter-clockwise spiral
  • Start from a different corner (top-right, bottom-left)
  • Adjust %4d to %5d for bigger matrices

Avoid

  • Using very large size on the stack (may overflow)
  • Forgetting to update boundaries (will cause overwrites)

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful