Given a matrix, print its elements in clockwise spiral order. This is the reverse of the first program: it reads a spiral instead of writing one. The logic is explained step by step, and the full program is given in C, C++, Java and Python.
Quick answer: Use the same four boundaries as the spiral matrix, but print each element as you visit it instead of storing a value. Walk the top row, the right column, the bottom row backwards and the left column upwards, shrink the boundaries and repeat. For the 4 × 4 example the output is 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10.
This program is part of the Spiral Patterns collection.
Table of contents8 sections
Print a Matrix in Spiral Order output
Output (4 × 4 matrix 1 to 16):
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

How the print a matrix in spiral order works
Use the same four boundaries as the spiral matrix, but print each element instead of assigning to it. The if checks stop the bottom row and left column from being printed twice when only one row or column is left.
Key takeaways
- The visiting order is exactly the fill order of the spiral number matrix.
- The
ifchecks are what make it work for any shape, including a single row or column. - Every element is printed exactly once: rows × columns values in total.
- The loop stops as soon as
top > bottomorleft > right.
Print a Matrix in Spiral Order in C
#include <stdio.h>
int main(void) {
int a[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int top = 0, bottom = 3, left = 0, right = 3;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++)
printf("%d ", a[top][j]);
top++;
for (int i = top; i <= bottom; i++)
printf("%d ", a[i][right]);
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--)
printf("%d ", a[bottom][j]);
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
printf("%d ", a[i][left]);
left++;
}
}
printf("\n");
return 0;
}Print a Matrix in Spiral Order in C++
#include <iostream>
using namespace std;
int main() {
int a[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int top = 0, bottom = 3, left = 0, right = 3;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++)
cout << a[top][j] << " ";
top++;
for (int i = top; i <= bottom; i++)
cout << a[i][right] << " ";
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--)
cout << a[bottom][j] << " ";
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
cout << a[i][left] << " ";
left++;
}
}
cout << endl;
return 0;
}Print a Matrix in Spiral Order in Java
public class PrintMatrixInSpiralOrder {
public static void main(String[] args) {
int[][] a = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
int top = 0, bottom = 3, left = 0, right = 3;
while (top <= bottom && left <= right) {
for (int j = left; j <= right; j++)
System.out.print(a[top][j] + " ");
top++;
for (int i = top; i <= bottom; i++)
System.out.print(a[i][right] + " ");
right--;
if (top <= bottom) {
for (int j = right; j >= left; j--)
System.out.print(a[bottom][j] + " ");
bottom--;
}
if (left <= right) {
for (int i = bottom; i >= top; i--)
System.out.print(a[i][left] + " ");
left++;
}
}
System.out.println();
}
}Print a Matrix in Spiral Order in Python
a = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
top, bottom, left, right = 0, 3, 0, 3
while top <= bottom and left <= right:
for j in range(left, right + 1):
print(a[top][j], end=" ")
top += 1
for i in range(top, bottom + 1):
print(a[i][right], end=" ")
right -= 1
if top <= bottom:
for j in range(right, left - 1, -1):
print(a[bottom][j], end=" ")
bottom -= 1
if left <= right:
for i in range(bottom, top - 1, -1):
print(a[i][left], end=" ")
left += 1
print()Time and space complexity
Every element is visited exactly once, so the traversal takes O(rows × columns) time and needs only O(1) extra memory besides the matrix itself.
Frequently asked questions
Does it work for rectangular matrices?
Yes. The boundaries are independent, so a 3 × 5 matrix works the same way; set bottom and right to the last row and column index.
What is the output for a 3 × 3 matrix holding 1 to 9?
1 2 3 6 9 8 7 4 5: the outer ring first, then the centre.
How do I collect the values instead of printing them?
Append each element to a list or array at the point where the program prints it; the order stays the same.








