C++ Sequential Alphabet Triangle Pattern (Decreasing)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A sequential decreasing alphabet triangle feeds one continuous letter stream across shrinking rows — the first row is longest, and each later row prints one fewer letter.

Remember
Rule: k starts at A and never resets;
      row i prints (n - i + 1) letters from k++

A B C D E
F G H I
J K L
M N
O     ← n = 5

Total letters equal the triangular number n(n+1)/2 (15 letters for five rows: A through O).

How to Solve It

Keep one shared counter outside the outer loop; shrink how many times the inner loop runs each row.

MethodIdeaBest for
Shrink with jInner j = n..i; print k++ each timeLearning, interviews, exams
Explicit countPrint n - i + 1 letters from the same streamClearer demos once the shrink clicks

Pseudocode

Pseudocode
k = 'A'
n = 5
for i from 1 to n:
    for j from n down to i:
        print k and a space
        k = next letter
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (int i = 1; i <= n; i++)
Shrink letter countfor (int j = n; j >= i; j--)
Print next lettercout << setw(2) << k++ << " ";
End the rowcout << "\n";
Letters per rowcount = n - i + 1;

Printing Letters vs Starting a New Line

APIEffectUse for
cout << k++One letter; stays on the line; advances the streamEach cell
cout << " "Separator between lettersReadable columns
cout << "\n"Ends the current lineAfter the letter loop

setw(2) needs #include <iomanip>. cout << endl also ends the line (and flushes); "\n" is enough for these demos.

Live Preview

Change the row count and the triangle updates instantly — capped at 6 so total letters stay within A–Z.

Whole numbers from 1 to 6. With n = 5 you get the classic A–O sample.

Live result 5 rows · 15 letters
A B C D E
F G H I
J K L
M N
O

Worked Walkthrough — n = 3

Three rows so the shared counter and shrinking lengths are easy to check by hand.

Row iLetters this rowk values usedPrinted row
13A, B, CA B C
22D, ED E
31FF

After row 1, k is already at D — it does not restart at A.

C++ Programs

Three complete programs: fixed five rows, cin row count, and an explicit letters-per-row rewrite. Use View Output to reveal sample results.

Example 1 — Fixed 5 rows

One counter k supplies letters; the inner loop decides how many times to print it in each row.

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

int main()
{
    char k = 'A';

    for (int i = 1; i <= 5; i++)
    {
        for (int j = 5; j >= i; j--)
            cout << setw(2) << k++ << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Keep one stream. k starts at 'A' outside the outer loop so it never resets.

2. Outer loop picks the row. i runs from 1 to 5.

3. Inner loop shrinks. j runs from 5 down to i — five letters, then four, then three, …

4. Break the line. cout << "\n" after the letter loop; the next row continues the same k.

Example 2 — User Input Version

Read the row count with cin. Cap so n(n+1)/2 ≤ 26 if you want only A–Z.

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

int main()
{
    int n;
    char k = 'A';

    cout << "Enter number of rows (like 5): ";
    cin >> n;
    if (n < 1) return 0;

    for (int i = 1; i <= n; i++)
    {
        for (int j = n; j >= i; j--)
            cout << setw(2) << k++ << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. cin >> n sets both loop bounds.

2. Same stream and shrink. Only n changes; k still continues across rows.

3. Letter budget. Total letters are n(n+1)/2 — for A–Z demos keep that ≤ 26.

4. Safer input tip. Reject bad or oversized values:

Safer input
if (!(cin >> n) || n < 1 || n > 6)
{
    cout << "Enter a whole number from 1 to 6.\n";
    return 1;
}

Example 3 — Explicit count per row

Same shape with count = n - i + 1 — often easier to read than the j shrink loop.

C++
#include <iostream>
using namespace std;

int main()
{
    int n = 5;
    char k = 'A';

    for (int i = 1; i <= n; i++)
    {
        int count = n - i + 1;
        for (int t = 0; t < count; t++)
            cout << k++ << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Name the length. Row i prints exactly n - i + 1 letters.

2. Same continuous stream. k++ still advances across every cell and every row.

3. Spacing is simpler. No setw here — just letter + space. Use Examples 1–2 when you want width formatting.

Edge Cases & Pitfalls

Check these before calling the solution done.

Reset k

Every row starts at A

Declare k outside the outer loop. Resetting it rebuilds a different pattern.

Growing rows

Wrong triangle direction

This pattern shrinks (n, n-1, …, 1). Growing rows is the opposite shape.

\n early

Broken rows

Call cout << "\n" only after the letter loop finishes.

n = 1

Single A

Output is just A — one row, one letter.

Past Z

Letter budget

Cap n so n(n+1)/2 ≤ 26, or define a wrap/error policy.

cin

Check failure

Use if (!(cin >> n)) so bad input does not leave n uninitialized.

Time and Space Complexity

ProgramTimeExtra space
Shrink loop (Examples 1–2)O(n²)O(1)
Explicit count (Example 3)O(n²)O(1)

For n rows the letter count is n + (n - 1) + … + 1 = n(n+1)/2 — quadratic in n.

Key Takeaways

  • One stream: keep k outside the outer loop so letters continue across rows.
  • Shrink each row: print n - i + 1 letters (or loop j = n..i).
  • Break the row: call cout << "\n" only after the letter loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i, print n - i + 1 letters from a shared k++ stream, then cout << "\n".

Frequently Asked Questions

Because the pattern is one continuous alphabet stream A through O. Resetting k would restart each row from A.
So the sequence continues across rows (A, B, C, …). If you reset k each row, every row would start at A again.
For the fixed 5-row example, the row lengths are 5, 4, 3, 2, 1. Each new row prints one fewer letter.
cout << k stays on the same line for each letter. cout << "\n" ends the row after the letter loop finishes.
The width-2 formatting aligns the columns (best in a monospace terminal). Adding a trailing space makes the output match the sample with spaces between letters.
Both use a continuous k++ stream. Program 22 grows rows and right-aligns with pad cells; this pattern shrinks row length each time and stays left-aligned.
O(n²) for n rows because total prints are n+(n−1)+…+1 = n(n+1)/2.
After cin >> n, check failure, require n ≥ 1, and cap so n(n+1)/2 ≤ 26 if you want only A–Z letters.

Did you know?

One counter k starts at A and never resets. The outer loop controls row count; the inner loop length decreases each row (5, 4, 3, 2, 1). Using setw(2) (and a trailing space) keeps a clean column layout in monospace output.

Next: Rotating Alphabet

Print rows that rotate the alphabet left each line.

Program 26 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful