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

Snake (Zig-Zag) Floyd’s Triangle

Read summary with AI tools

The snake Floyd’s triangle, also called the zig-zag Floyd’s triangle, uses the same numbers as Floyd’s triangle, but every second row is printed backwards. Reading it is like following a snake: right on row 1, left on row 2, right on row 3.

Quick answer: For row i, work out its first and last numbers: first = i(i − 1) / 2 + 1 and last = i(i + 1) / 2. On odd rows print from first upwards; on even rows print from last downwards. The i % 2 test picks the direction.

This program is part of the Series Patterns collection.

Table of contents8 sections
  1. Snake Floyd’s Triangle output
  2. How the snake floyd’s triangle works
  3. Snake Floyd’s Triangle in C
  4. Snake Floyd’s Triangle in C++
  5. Snake Floyd’s Triangle in Java
  6. Snake Floyd’s Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Snake Floyd’s Triangle output

Output (n = 5):

  1
  3  2
  4  5  6
 10  9  8  7
 11 12 13 14 15
Snake Floyd's Triangle layout diagram, row by row (n = 5)
The numbers of the snake floyd’s triangle laid out row by row (n = 5).

How the snake floyd’s triangle works

Each row of Floyd’s triangle holds a known block of numbers. Row i starts right after the i(i − 1) / 2 numbers used by the rows above it, so it covers first to last as given above. Once you know both ends, the direction is a simple choice: count up from first on odd rows and down from last on even rows. This zig-zag order is also called boustrophedon, after the way an ox ploughs a field.

  1. Run the outer loop for i from 1 to n.
  2. Work out first = i(i − 1) / 2 + 1 and last = i(i + 1) / 2.
  3. For j from 0 to i − 1: print first + j if i is odd, otherwise last − j.
  4. Move to the next line.

Key takeaways

  • Every row covers a fixed block of numbers, from i(i − 1) / 2 + 1 to i(i + 1) / 2.
  • Only the direction changes between rows; the numbers themselves are the same as in Floyd’s triangle.
  • The test i % 2 == 1 sends odd rows forwards and even rows backwards.
  • The same zig-zag idea drives the snake number pattern on a square.

Snake Floyd’s Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Snake Floyd’s Triangle in C++

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

int main() {
    int n = 5;

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

Snake Floyd’s Triangle in Java

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

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

Snake Floyd’s Triangle in Python

n = 5

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

Time and space complexity

Each number is printed once with constant work, so the program runs in O(n²) time and uses O(1) extra memory.

Frequently asked questions

Can the first row go right to left instead?

Yes. Swap the two branches so that odd rows count down from last and even rows count up from first. With one number on row 1 it looks the same; the change shows from row 2 onwards.

Is there a way to do it without the formulas?

Keep a running counter and store each row in a small array, then print the array forwards or backwards. It works, but the two formulas are shorter and need no extra memory.

Where else is the zig-zag order used?

It shows up in matrix problems such as the zig-zag (spiral) level order of a binary tree and in the snake number pattern. Once you can reverse every other row, those problems are mostly the same trick.

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.