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

Snake Number Pattern

Read summary with AI tools
Table of contents
  1. Output
  2. How the snake number pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Related patterns

Numbers 1 to n² run left to right on odd rows and right to left on even rows, like a snake. Below you will see how the pattern is built, followed by complete programs in C, C++, Java and Python.

This program is part of the Wave Patterns collection.

Snake Number Pattern output

Output (n = 4):

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

How the snake number pattern works

On odd rows the value is (i − 1)·n + j. On even rows the row is reversed, so the value is i·n − j + 1.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n positions in the row. Print numbers (value (i − 1) × n + j) where i % 2 = 1; print numbers (value i × n − j + 1) everywhere else.
  3. Move to the next line.

Snake Number Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i % 2 == 1)
                printf("%3d", (i - 1) * n + j);
            else
                printf("%3d", i * n - j + 1);
        }
        printf("\n");
    }
    return 0;
}

Snake Number Pattern in C++

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i % 2 == 1)
                cout << setw(3) << (i - 1) * n + j;
            else
                cout << setw(3) << i * n - j + 1;
        }
        cout << endl;
    }
    return 0;
}

Snake Number Pattern in Java

public class SnakeNumberWave {
    public static void main(String[] args) {
        int n = 4;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i % 2 == 1)
                    System.out.printf("%3d", (i - 1) * n + j);
                else
                    System.out.printf("%3d", i * n - j + 1);
            }
            System.out.println();
        }
    }
}

Snake Number Pattern in Python

n = 4

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i % 2 == 1:
            print(f"{(i - 1) * n + j:3d}", end="")
        else:
            print(f"{i * n - j + 1:3d}", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

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.

Latest articles

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.