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
Alphabet Spiral Matrix output
Output (n = 4):
A B C D L M N E K P O F J I H G

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.
- Fill an
n × ngrid with zeros and start in the top-left cell. - For each value from 1 to the last one: write it in the current cell.
- 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.
- 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 − 1for thev-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.






