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
Snake Floyd’s Triangle output
Output (n = 5):
1 3 2 4 5 6 10 9 8 7 11 12 13 14 15

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.
- Run the outer loop for
ifrom 1 ton. - Work out
first = i(i − 1) / 2 + 1andlast = i(i + 1) / 2. - For
jfrom 0 toi − 1: printfirst + jifiis odd, otherwiselast − j. - Move to the next line.
Key takeaways
- Every row covers a fixed block of numbers, from
i(i − 1) / 2 + 1toi(i + 1) / 2. - Only the direction changes between rows; the numbers themselves are the same as in Floyd’s triangle.
- The test
i % 2 == 1sends 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.










