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

Column-Wise Alphabet Triangle

The column-wise alphabet triangle fills the letters down the columns instead of across the rows: the first column reads A to F, the second G to K, the third L to O, and so on to U.

Quick answer: Column j holds n − j + 1 letters, so the columns before it use (j − 1)n − (j − 1)j/2 letters. The letter at row i, column j is therefore 'A' + i − 1 + (j − 1)n − (j − 1)j/2. One formula, no array.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the column-wise alphabet triangle 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-Wise Alphabet Triangle output

Output (n = 6):

A
B G
C H L
D I M P
E J N Q S
F K O R T U
Column-Wise Alphabet Triangle layout diagram, row by row (n = 6)
How each row of the column-wise alphabet triangle is built (n = 6). Blue cells are printed characters; grey dots are spaces.

How the column-wise alphabet triangle works

A console prints row by row, so the program has to know the letter in each cell without filling the columns first. The first column is just the row number. Each column to the right starts after all the letters of the columns before it, and those columns get one letter shorter each time: 6, 5, 4 and so on. Adding up that shrinking series gives the formula above. It is the alphabet twin of the column-wise Floyd’s triangle.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print i letters (letter number i + (j − 1) × n − (j − 1) × j / 2).
  3. Move to the next line.

Key takeaways

  • The letters run down each column, not across each row.
  • Column j holds n − j + 1 letters.
  • The offset (j − 1)n − (j − 1)j/2 counts the letters in earlier columns.
  • Six rows use 21 letters, A to U.

Column-Wise Alphabet Triangle in C

#include <stdio.h>

int main(void) {
    int n = 6;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            printf("%c ", 'A' + i + (j - 1) * n - (j - 1) * j / 2 - 1);
        printf("\n");
    }
    return 0;
}

Column-Wise Alphabet Triangle in C++

#include <iostream>
using namespace std;

int main() {
    int n = 6;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << char('A' + i + (j - 1) * n - (j - 1) * j / 2 - 1) << " ";
        cout << endl;
    }
    return 0;
}

Column-Wise Alphabet Triangle in Java

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

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print((char) ('A' + i + (j - 1) * n - (j - 1) * j / 2 - 1) + " ");
            System.out.println();
        }
    }
}

Column-Wise Alphabet Triangle in Python

n = 6

for i in range(1, n + 1):
    for j in range(1, i + 1):
        print(chr(ord('A') + i + (j - 1) * n - (j - 1) * j // 2 - 1), 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).

Frequently asked questions

How do I check the formula?

Take row 4, column 3 with n = 6: 4 + (3 − 1) × 6 − (3 − 1) × 3 / 2 = 4 + 12 − 3 = 13. The 13th letter is M, which is exactly what the output shows in that cell.

How many rows fit before the alphabet runs out?

Six rows use 21 letters. Seven rows would need 28, so stop at six or wrap around with % 26.

Can I use a two-dimensional array instead?

Yes: fill an array column by column with a counter, then print it row by row. The formula avoids the array.

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 15 of 22 in String 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.