C++ Inverted Alphabet Triangle Pattern

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

What Is This Pattern?

An inverted alphabet right-angled triangle starts with the longest prefix and drops one letter from the end on each next row — while every row still begins at A.

Remember
Rule: on row with end i, print A..i (i shrinks)

ABCDE
ABCD
ABC
AB
A         ← 5 rows (top = 'E')

This is Program 1 flipped: only the outer loop counts down. Compare also with Program 4 (grows while printing backward) and Program 6 (shifts the start letter).

How to Solve It

Two ways to emit the same shape — classic nested char loops, or spaced letters for readability.

MethodIdeaBest for
Nested char loopsOuter i-- from top; inner prints A..iLearning, interviews, exams
Spaced lettersSame bounds; print j << " "Clearer console demos once loops click

Pseudocode

Pseudocode
top = 'A' + rows - 1
for i from top down to 'A':
    for j from 'A' to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Top letterchar top = char('A' + rows - 1);
Shrink rowsfor (char i = top; i >= 'A'; i--)
Print A..ifor (char j = 'A'; j <= i; j++) cout << j;
End the rowcout << "\n";
Grow insteadfor (char i = 'A'; i <= top; i++) → Program 1
Letter totaln + (n−1) + … + 1 = n(n+1)/2

Printing Letters vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach letter
cout << "\n"Ends the current lineAfter the inner loop finishes a row

Print letters without a newline, then end the row once.

Live Preview

Change the row count and the inverted triangle updates instantly — including the triangular letter total.

Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · top E · 15 letters
ABCDE
ABCD
ABC
AB
A

Worked Walkthrough — Top = E (5 rows)

Trace each end letter i and the forward prefix from A.

i (end)Inner rangePrinted rowLetters
EA..EABCDE5
DA..DABCD4
CA..CABC3
BA..BAB2
AA..AA1

Total letter prints: 5 + 4 + 3 + 2 + 1 = 15 = 5×6/2. The left edge is always A; that triangular sum is why time is O(n²).

C++ Programs

Three complete programs: fixed A–E, row-count input, and a spaced-letter variant. Use View Output to reveal sample results.

Example 1 — Fixed Top E

Hard-coded height — outer loop shrinks the end letter; inner loop always prints from A.

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

int main()
{
    for (char i = 'E'; i >= 'A'; i--)
    {
        for (char j = 'A'; j <= i; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop shrinks the width. i runs from 'E' down to 'A' — first row longest, last row a single letter.

2. Inner loop always starts at A. For each i, j runs from 'A' to i, so every row begins with A.

3. Break the line. cout << "\n" after the inner loop starts the next (shorter) row.

When i = 'C' you get ABC; when i = 'A' you get A.

Example 2 — Row Count Input

Read the number of rows and compute top = 'A' + rows - 1. Prefer validating cin in real apps.

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

int main()
{
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    char top = char('A' + rows - 1);
    for (char i = top; i >= 'A'; i--)
    {
        for (char j = 'A'; j <= i; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Scale with rows. For 4 rows, top becomes 'D'. Cap rows at 26 so top stays within A–Z.

2. Same nested-loop core. Only the source of top changes — the print logic matches Example 1.

3. Safer input tip. Prefer:

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

Example 3 — Spaced Letters

Same bounds — print a trailing space after each letter so columns are easier to scan.

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

int main()
{
    char top = 'E';

    for (char i = top; i >= 'A'; i--)
    {
        for (char j = 'A'; j <= i; j++)
            cout << j << " ";
        cout << "\n";
    }

    return 0;
}

How It Works

1. Bounds unchanged. Outer still shrinks i; inner still walks A..i.

2. Only the printed unit changes. Each cell becomes j + " ". Trim trailing spaces later if you need a compact line.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Growing triangle

If the outer loop goes from 'A' up to top, you get Program 1 (A, AB, ABC…). Use i-- from top for the inverted shape.

\n early

Column of letters

If cout << "\n" is inside the inner loop, each letter lands on its own line. Print letters without a newline; end the row only after the inner loop.

j from i

Wrong cousin pattern

Starting the inner loop at i instead of 'A' produces a different pattern (like Program 6). Always restart at 'A'.

rows = 1

Single A

Output is just A on one line — a good sanity check.

rows > 26

Beyond Z

Cap or reject — top leaves the alphabet. Keep rows in 1…26 for A–Z demos.

Bad cin

Validate rows

Check cin >> rows, require a positive whole number, and clamp to 26 before building top.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
Spaced letters (Example 3)O(n²)O(1)

Total letters printed = n + (n−1) + … + 1 = n(n+1)/2, which is still quadratic in n.

Key Takeaways

  • Rule: first row longest; each next row drops one letter from the end.
  • Two loops: outer i-- from top; inner always prints A..i.
  • Break the row: cout << "\n" only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each end letter i from top down to A, print A..i, then a newline.

Frequently Asked Questions

The outer loop counts down so the first row prints the most letters and each next row prints one fewer. The inner loop still prints from A up to the current bound.
Because the inner loop always starts at 'A'. That resets the sequence each row, producing ABCDE then ABCD and so on.
Program 1 grows row length (A, AB, ABC). This pattern shrinks it (ABCDE, ABCD, ABC). Only the outer loop direction changes; the inner loop is the same A..i print.
Program 4 grows while printing backward (A, BA, CBA). This pattern shrinks while printing forward from A (ABCDE, ABCD, ABC).
O(n²) for n rows, because total printed characters are n+(n−1)+…+1 = n(n+1)/2.
Yes. Read rows from cin, set top = char('A' + rows - 1), then loop i from top down to 'A' and print j from 'A' up to i.
After cin >> rows, check failure, require n ≥ 1, and cap at 26 so the top letter stays within A–Z.
Yes. Use 'a' as the base: top = char('a' + rows - 1), then loop the same way with j from 'a' up to i.

Did you know?

This is Program 1 flipped: for 5 rows it prints ABCDE, ABCD, ABC, AB, A. Only the outer loop direction changes — the inner loop still prints forward from 'A'.

Next: Increasing Start Letter

Keep the same end letter each row, but start one letter later — ABCDE, BCDE, CDE…

Program 6 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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