New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Alphabet Spiral Matrix

The alphabet spiral matrix writes the letters A to P around a 4 × 4 grid in a clockwise spiral: along the top, down the right side, back along the bottom and up the left, winding in towards the centre.

Quick answer: Fill the grid with the numbers 1 to n² in a clockwise spiral, then print each number v as the letter 'A' + v − 1. The spiral logic stays the same; only the printing turns numbers into letters.

This program is part of the Spiral Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet spiral matrix works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Alphabet Spiral Matrix output

Output (n = 4):

A B C D
L M N E
K P O F
J I H G
Alphabet Spiral Matrix layout diagram, row by row (n = 4)
How each row of the alphabet spiral matrix is built (n = 4). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Alphabet Square Pattern program draws this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the alphabet spiral matrix works

Keeping the walk and the printing separate pays off here. The walk fills the grid with positions in spiral order, exactly as for the spiral number matrix. Turning a number into a letter is one addition at print time. That separation also makes it easy to print the spiral as numbers, letters or symbols from the same filled grid.

  1. Fill an n × n grid with zeros and start in the top-left cell.
  2. For each value from 1 to the last one: write it in the current cell.
  3. Try to step in the current direction. If that cell is outside the grid or already filled, turn to the next direction and try again.
  4. Print each cell as the letter 'A' + a[i][j] − 1, followed by a space.

Key takeaways

  • The walk fills positions; the printing decides how they look.
  • Letter = 'A' + v − 1 for the v-th cell of the spiral.
  • A 5 × 5 grid needs 25 letters, A to Y; 6 × 6 would run past Z.
  • Reading the letters in order traces the spiral path.

Alphabet Spiral Matrix in C

#include <stdio.h>

int main(void) {
    int n = 4;
    int a[20][20] = {0};                    /* n can be up to 20 */
    int dr[] = {0, 1, 0, -1};                  /* right, down, left, up: clockwise */
    int dc[] = {1, 0, -1, 0};
    int r = 0, c = 0, d = 0;

    for (int value = 1; value <= n * n; value++) {
        a[r][c] = value;
        for (int turn = 0; turn < 4; turn++) {   /* find the next free cell */
            int nr = r + dr[d], nc = c + dc[d];
            if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
                r = nr;
                c = nc;
                break;
            }
            d = (d + 1) % 4;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            printf("%c ", 'A' + a[i][j] - 1);
        printf("\n");
    }
    return 0;
}

Alphabet Spiral Matrix in C++

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

int main() {
    int n = 4;
    vector<vector<int>> a(n, vector<int>(n, 0));
    int dr[] = {0, 1, 0, -1};                  // right, down, left, up: clockwise
    int dc[] = {1, 0, -1, 0};
    int r = 0, c = 0, d = 0;

    for (int value = 1; value <= n * n; value++) {
        a[r][c] = value;
        for (int turn = 0; turn < 4; turn++) {   // find the next free cell
            int nr = r + dr[d], nc = c + dc[d];
            if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
                r = nr;
                c = nc;
                break;
            }
            d = (d + 1) % 4;
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << char('A' + a[i][j] - 1) << " ";
        cout << endl;
    }
    return 0;
}

Alphabet Spiral Matrix in Java

public class AlphabetSpiralMatrix {
    public static void main(String[] args) {
        int n = 4;
        int[][] a = new int[n][n];
        int[] dr = {0, 1, 0, -1};                  // right, down, left, up: clockwise
        int[] dc = {1, 0, -1, 0};
        int r = 0, c = 0, d = 0;

        for (int value = 1; value <= n * n; value++) {
            a[r][c] = value;
            for (int turn = 0; turn < 4; turn++) {   // find the next free cell
                int nr = r + dr[d], nc = c + dc[d];
                if (nr >= 0 && nr < n && nc >= 0 && nc < n && a[nr][nc] == 0) {
                    r = nr;
                    c = nc;
                    break;
                }
                d = (d + 1) % 4;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++)
                System.out.print((char) ('A' + a[i][j] - 1) + " ");
            System.out.println();
        }
    }
}

Alphabet Spiral Matrix in Python

n = 4
a = [[0] * n for _ in range(n)]
dr = [0, 1, 0, -1]                          # right, down, left, up: clockwise
dc = [1, 0, -1, 0]
r = c = d = 0

for value in range(1, n * n + 1):
    a[r][c] = value
    for turn in range(4):              # find the next free cell
        nr, nc = r + dr[d], c + dc[d]
        if 0 <= nr < n and 0 <= nc < n and a[nr][nc] == 0:
            r, c = nr, nc
            break
        d = (d + 1) % 4

for i in range(n):
    for j in range(n):
        print(chr(64 + a[i][j]) + " ", end="")
    print()

Time and space complexity

The program fills and prints n² cells, so it runs in O(n²) time with O(n²) memory.

Frequently asked questions

What happens for n = 6?

A 6 × 6 grid has 36 cells but the alphabet has 26 letters. Wrap around with (v − 1) % 26 to start again at A.

Can I write a word along the spiral?

Yes. Print word[(v − 1) % length] instead of a letter, and the word repeats along the path.

How do I make the spiral anticlockwise?

Change the direction order to down, right, up, left, as in the anticlockwise spiral matrix.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 6 of 14 in Spiral Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.