The wave print of a matrix reads it down the first column, up the second, down the third and so on. For the grid 1 to 16 the wave reads 1 5 9 13 14 10 6 2 3 7 11 15 16 12 8 4. It is a common coding-test question.
Quick answer: Loop over the columns. For an even column read the rows from top to bottom; for an odd column read them from bottom to top. Print each element as you reach it, all on one line. For the 4 × 4 grid the first column gives 1 5 9 13 and the second column, read upwards, gives 14 10 6 2.
This program is part of the Wave Patterns collection.
Table of contents8 sections
Wave Print of a Matrix output
Output (4 × 4 matrix 1 to 16):
1 5 9 13 14 10 6 2 3 7 11 15 16 12 8 4

How the wave print of a matrix works
The traversal only changes direction between columns, so the outer loop goes over the columns and an if picks the direction of the inner loop. It is the reading counterpart of the column snake number square, which writes numbers in the same order.
- Fill the matrix with 1 to
n²row by row. - For each column
j: ifjis even, print rows 0 ton − 1; otherwise print rowsn − 1down to 0. - End with a new line.
Key takeaways
- The direction flips on every column.
- The outer loop runs over columns, the inner loop over rows.
- Every element is printed once, in a single line.
- Swap the loops to get the row-wise wave: left to right, then right to left.
Wave Print of a Matrix in C
#include <stdio.h>
int main(void) {
int n = 4;
int a[4][4];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
for (int i = 0; i < n; i++) /* down this column */
printf("%d ", a[i][j]);
else
for (int i = n - 1; i >= 0; i--) /* up this column */
printf("%d ", a[i][j]);
}
printf("\n");
return 0;
}Wave Print of a Matrix in C++
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n = 4;
vector<vector<int>> a(n, vector<int>(n));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
for (int i = 0; i < n; i++) // down this column
cout << a[i][j] << " ";
else
for (int i = n - 1; i >= 0; i--) // up this column
cout << a[i][j] << " ";
}
cout << endl;
return 0;
}Wave Print of a Matrix in Java
public class WavePrintMatrix {
public static void main(String[] args) {
int n = 4;
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = i * n + j + 1;
for (int j = 0; j < n; j++) {
if (j % 2 == 0)
for (int i = 0; i < n; i++) // down this column
System.out.print(a[i][j] + " ");
else
for (int i = n - 1; i >= 0; i--) // up this column
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}Wave Print of a Matrix in Python
n = 4
a = [[i * n + j + 1 for j in range(n)] for i in range(n)]
result = []
for j in range(n):
rows = range(n) if j % 2 == 0 else range(n - 1, -1, -1) # down even columns, up odd ones
for i in rows:
result.append(a[i][j])
print(" ".join(str(x) for x in result))Time and space complexity
Every element is printed once, so the program runs in O(n²) time and O(1) extra memory (the Python version collects the output in a list).
Frequently asked questions
How do I print it row-wise instead?
Make the rows the outer loop and alternate the column direction: 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13.
Does it work for rectangular matrices?
Yes. Loop over the number of columns outside and the number of rows inside; nothing else changes.
How is it related to spiral order?
Both read every element once in a fixed path. The spiral order goes round the edges; the wave goes up and down the columns.






