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

Column Snake Number Square

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
  1. Output
  2. How the column snake number square 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

Column Snake Number Square output

Output (n = 4):

  1  8  9 16
  2  7 10 15
  3  6 11 14
  4  5 12 13
Column Snake Number Square layout diagram, row by row (n = 4)
The numbers of the column snake number square laid out row by row (n = 4).

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 + 1 to (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.

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 7 of 12 in Wave 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.