The column snake number square counts down the first column, back up the second, down the third and so on, so the numbers 1 to 16 weave through the grid like a snake. It is the column version of the snake number pattern.
Quick answer: Column j (counted from 0) holds the numbers jn + 1 to (j + 1)n. In even columns they count down the page, so row i gets jn + i + 1; in odd columns they count back up, so row i gets jn + n − i.
This program is part of the Wave Patterns collection.
Table of contents8 sections
Column Snake Number Square output
Output (n = 4):
1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13

How the column snake number square works
A console prints across rows, but the numbers here run down columns, so each value has to be worked out from its row and column directly. Every column holds a block of n consecutive numbers starting after jn. Which end of the block sits at the top depends on whether the column is even or odd. That one choice is what makes the snake.
Key takeaways
- Each column holds the block
jn + 1to(j + 1)n. - Even columns count down the page; odd columns count back up.
- The value is worked out directly, so no array is needed.
- Swap rows and columns in the formula to get the row-wise snake.
Column Snake Number Square in C
#include <stdio.h>
int main(void) {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int value = (j % 2 == 0) ? j * n + i + 1 /* even columns count down the page */
: j * n + n - i; /* odd columns count back up */
printf("%3d", value);
}
printf("\n");
}
return 0;
}Column Snake Number Square in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int value = (j % 2 == 0) ? j * n + i + 1 // even columns count down the page
: j * n + n - i; // odd columns count back up
cout << setw(3) << value;
}
cout << endl;
}
return 0;
}Column Snake Number Square in Java
public class ColumnSnakeNumberSquare {
public static void main(String[] args) {
int n = 4;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int value = (j % 2 == 0) ? j * n + i + 1 // even columns count down the page
: j * n + n - i; // odd columns count back up
System.out.printf("%3d", value);
}
System.out.println();
}
}
}Column Snake Number Square in Python
n = 4
for i in range(n):
for j in range(n):
if j % 2 == 0:
value = j * n + i + 1 # even columns count down the page
else:
value = j * n + n - i # odd columns count back up
print(f"{value:3d}", end="")
print()Time and space complexity
Each of the n² values is computed in constant time, so the program runs in O(n²) time with O(1) extra memory.
Frequently asked questions
How is it different from the snake number pattern?
The snake number pattern weaves left and right along the rows. This one weaves up and down along the columns.
How do I check a value quickly?
Take row 2, column 1 (both counted from 0). Column 1 is odd, so the value is 1 × 4 + 4 − 2 = 6, which matches the output: the second column reads 8, 7, 6, 5 from the top.
Can I start the first column at the bottom?
Yes. Swap the two formulas: even columns then count up from the bottom and odd columns count down.
How do I let the user choose the size?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






