C++ Alphabet Triangle Pattern (Right-Angled)

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

What Is This Pattern?

An alphabet right-angled triangle prints a left-aligned staircase of letters: row i runs from A through the i-th letter (A, AB, ABC, …).

Remember
Rule: on row i, print A through the i-th letter

A
AB
ABC
ABCD
ABCDE     ← 5 rows

In C++ you solve it with two nested for loops over char: the outer loop picks the end letter, the inner loop restarts at A and prints through that end letter, then cout << "\n" moves to the next line. Once this clicks, inverted letter triangles and other alphabet shapes become much easier.

How to Solve It

Two ways to emit the same shape — start with nested char loops and cout, then optionally polish with substr.

MethodIdeaBest for
Nested loops + coutOuter = end letter; inner = A..end via cout << jLearning, interviews, exams
substr(0, i)Slice an A–Z string for each row lengthShort demos once loops click

Pseudocode

Pseudocode
for end from 'A' to lastLetter:   // lastLetter depends on rows
    for ch from 'A' to end:
        print ch (no newline)
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (char i = 'A'; i <= last; i++)
Print A..ifor (char j = 'A'; j <= i; j++) cout << j;
End the rowcout << "\n";
Last letter from rowslast = (char)('A' + rows - 1);
substr shortcutcout << letters.substr(0, i) << "\n";
Invert laterShrink end letter each row → Program 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

Print characters without a newline, then end the row once. (cout << endl also ends the line (and flushes); "\n" is enough for these demos.

Live Preview

Change the row count and the alphabet triangle updates instantly — capped at 26 letters (A–Z).

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

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

Worked Walkthrough — rows = 4

Trace each outer-loop end letter and count how many times the inner loop prints.

End iInner jPrinted rowLetters
'A'A..AA1
'B'A..BAB2
'C'A..CABC3
'D'A..DABCD4

Total letter prints: 1 + 2 + 3 + 4 = 10 = 4×5/2. That triangular sum is why time is O(n²).

C++ Programs

Three complete programs: fixed end letter, cin input, and a substr shortcut. Use View Output to reveal sample results.

Example 1 — Fixed through 'E'

Hard-coded end letter — ideal for first demos and screenshots (5 rows).

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

int main() {
    char i, j;

    for (i = 'A'; i <= 'E'; i++) {
        for (j = 'A'; j <= i; j++) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop picks the end letter. i runs from 'A' to 'E' — one row per end letter.

2. Inner loop restarts at A. For each i, j runs from 'A' to i, so the row is A..i.

3. Print letters, then break the line. cout << j stays on the row; cout << "\n" after the inner loop starts the next row.

When i = 'A' you get A; when i = 'B' you get AB; up through ABCDE.

Example 2 — User Input Version

Read the row count at runtime with cin. Always check failure in real apps (shown in the tip below).

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

int main() {
    int rows;
    char i, j, last;

    cout << "Enter the number of rows: ";
    cin >> rows;
    last = (char)('A' + rows - 1);

    for (i = 'A'; i <= last; i++) {
        for (j = 'A'; j <= i; j++) {
            cout << j;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a row count, then store it with cin >> rows.

2. Map rows to an end letter. last = (char)('A' + rows - 1) — for rows = 4, last is 'D'.

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

4. Safer input tip. Unchecked cin leaves rows unset on bad input. Prefer:

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

Example 3 — substr(0, i)

Slice an A–Z string for each row length — same shape without an explicit inner letter loop.

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

int main() {
    int rows = 5;
    string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int i = 1; i <= rows; i++) {
        cout << letters.substr(0, i) << "\n";
    }

    return 0;
}

How It Works

1. One alphabet string. letters holds A through Z once.

2. Slice by row length. letters.substr(0, i) returns the first i characters — exactly row i.

3. Print the whole row. One cout per row replaces the inner letter loop.

Learn the nested-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat substr as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

\n inside

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.

No restart

Letters keep advancing

The inner loop must start at 'A' every row. A single advancing counter across rows produces a different pattern.

No newline

One endless line

Omitting the row break glues every letter onto a single line.

rows > 26

Past Z

'A' + rows - 1 leaves A–Z when rows > 26. Clamp or reject in interactive programs.

rows = 1

Single A

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

cin fail

Check the stream

If cin fails, rows may be unset — always test if (!(cin >> rows)) and prefer 1–26.

Time and Space Complexity

ProgramTimeExtra space
Nested loops + cout (Examples 1–2)O(rows²)O(1)
substr (Example 3)O(rows²)O(1) for the fixed alphabet string*

Total letters printed = 1 + 2 + … + n = n(n+1)/2, which is still quadratic in n. *Each substr builds a temporary of length i; overall work stays O(n²).

Key Takeaways

  • Rule: row with end letter i prints A through i.
  • Two loops: outer = end letter, inner = A..end with cout << j.
  • Break the row: call cout << "\n" only after the inner loop.
  • Complexity: O(n²) time from the triangular letter count; O(1) extra space for the loop form.

One line: for each end letter i, print A through i with cout, then cout << "\n".

Frequently Asked Questions

The outer loop picks the last letter on each row. The inner loop always restarts at A and prints up through that last letter, so row 1 is A, row 2 is AB, row 3 is ABC, and so on.
Each row is a fresh sequence from A to the current end letter. If you kept advancing a single char across rows, you would get a different pattern (not this right-angled alphabet triangle).
cout << j stays on the same line. cout << "\n" ends the current line. Letters use cout << j; the row break uses cout << "\n" after the inner loop.
Shrink the end letter each row — for example walk the outer loop from the last letter down toward A. See Alphabet Pattern Program 2.
O(n²) where n is the number of rows. Total letter prints equal 1+2+…+n = n(n+1)/2.
Yes. Keep a string of A–Z and print letters.substr(0, i) for row length i. Nested char loops are better for learning; substr is a handy shortcut later.
After cin >> rows, check failure: if (!(cin >> rows)) handle bad input. Clamp rows between 1 and 26 so you stay within A–Z.
Char math can produce characters beyond Z. Clamp to 26 for A–Z demos, or define a clear wrap/error policy.

Did you know?

Row i prints letters from A through the i-th letter (A, AB, ABC, …). Total letters for n rows is the triangular number n(n+1)/2 — the same count that makes this pattern O(n²).

Next: Inverted Alphabet Triangle

Flip the outer loop and shrink the end letter each row.

Program 2 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