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

Column-Wise Floyd’s Triangle

Read summary with AI tools

Column-wise Floyd’s triangle fills the numbers 1 to 15 down the columns instead of across the rows. The first column reads 1 2 3 4 5, the second 6 7 8 9, and so on.

Quick answer: Start each row with value = i, because the first column simply counts 1 to n. Then, for each step to the right, add the length of the column you just left: value += n − j. On row 3 that gives 3, then 3 + 4 = 7, then 7 + 3 = 10.

This program is part of the Series Patterns collection.

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

Column-Wise Floyd’s Triangle output

Output (n = 5):

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

How the column-wise floyd’s triangle works

You cannot print a column at a time on a console, so the program has to work out each row directly. Column j holds n − j + 1 numbers. Moving one column to the right therefore jumps over the rest of the current column and the start of the next one, which adds up to n − j. Starting from i in the first column and adding these gaps gives every value in row i without any array.

  1. Run the outer loop for i from 1 to n.
  2. Set value = i at the start of the row.
  3. For j from 1 to i: print value three characters wide, then add n − j to it.
  4. Move to the next line.

Key takeaways

  • The first column is just 1 to n, so each row starts at value = i.
  • The gap between neighbours shrinks by one per column: 4, 3, 2, 1 for five rows.
  • No array is needed even though the numbers run down the columns.
  • The last number is still n(n + 1) / 2, as in the normal Floyd’s triangle.

Column-Wise Floyd’s Triangle in C

#include <stdio.h>

int main(void) {
    int n = 5;

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

Column-Wise 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 value = i;
        for (int j = 1; j <= i; j++) {
            cout << setw(3) << value;
            value += n - j;
        }
        cout << endl;
    }
    return 0;
}

Column-Wise Floyd’s Triangle in Java

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

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

Column-Wise Floyd’s Triangle in Python

n = 5

for i in range(1, n + 1):
    value = i
    for j in range(1, i + 1):
        print(f"{value:3d}", end="")
        value += n - j
    print()

Time and space complexity

Each number takes one addition, so the program runs in O(n²) time for n rows and needs only O(1) extra memory.

Frequently asked questions

How is this different from Floyd’s triangle?

Floyd’s triangle fills the numbers row by row: 1, then 2 3, then 4 5 6. This version fills them column by column, so the rows read 1, 2 6, 3 7 10. Both use the same 15 numbers and the same triangle shape.

Why is the gap n − j?

Column j holds n − j + 1 numbers. From row i in column j to row i in the next column you pass the rest of column j and the first part of column j + 1. Those two pieces always add up to n − j.

Could I use a two-dimensional array instead?

Yes: fill an n × n array column by column with a counter, then print the lower triangle row by row. It is easier to picture but uses O(n²) memory, while the formula needs none.

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.