A repeating alphabet triangle grows by one character each row, but every character on that row is the same letter — the letter advances with the row.
Remember
Rule: for letter i from A to last,
print i with growing width 1, 2, 3, …
A
BB
CCC
DDDD
EEEEE ← 5 rows
Unlike Program 1 (A, AB, ABC), letters do not step across the row — you print the outer loop letter inside the inner loop. The reverse twin is Program 10 (E, DD, CCC, …).
Approach
How to Solve It
Two ways to emit the same shape — start with nested char loops, then optionally shorten with string(n, ch).
Method
Idea
Best for
Nested char loops
Outer = letter; inner = growing width; print outer letter
Learning, interviews, exams
string(row, ch)
Build a whole repeated-letter row in one call
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 'A' to lastLetter:
for j from 'A' to i:
print i (no newline)
print newline
1. Pick the letter from the row index. Same mapping as Example 2: row 1 → A, row 2 → B, and so on.
2. Fill a string of length row.string(row, ch) creates a string filled with ch — e.g. string(3, 'C') is CCC.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
cout << j
Program 1 by mistake
Printing j instead of i produces A, AB, ABC. Use cout << i (or ch) for a uniform row.
\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.
Off-by-one
Wrong letter mapping
Use char('A' + row - 1). Forgetting - 1 shifts every letter one step forward.
rows > 26
Past Z
'A' + row - 1 leaves A–Z when rows > 26. Clamp or reject in interactive programs.
rows = 1
Single A
Output is just A. A good sanity check for the mapping and the break.
Bad cin
Validate rows
Check cin >> rows and require 1–26 before the outer loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
string(row, ch) (Example 3)
O(rows²)
O(rows) per temporary row string
Total letters = 1 + 2 + … + n = n(n + 1)/2 — quadratic in n. Same totals as Program 1.
Remember
Key Takeaways
Rule: outer loop picks the letter; inner loop only sets how many times to print it.
Print i, not j: that single choice separates this pattern from Program 1.
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 nested loops.
One line: for each letter i, print i exactly as many times as the row width, then end the line.
Frequently Asked Questions
Because the inner loop prints the outer-loop character (i) every time. The inner counter only controls how many times to print, not which character to print.
Then letters would change across the row (A, AB, ABC…), which is Program 1 — not a repeating-letter triangle.
On the third row the row letter is C, and the inner loop runs three times, printing C each time.
Program 1 prints stepping letters across each row (A, AB, ABC). This pattern keeps one letter per row and only grows the repeat count (A, BB, CCC).
Program 10 uses the same growing widths but letters count downward (E, DD, CCC…). This pattern counts upward (A, BB, CCC…).
Printing a character stays on the same line. Printing a newline ends the current line. Letters use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows, because total printed characters are n(n+1)/2.
Yes. cout << string(row, ch) << "\n" prints a full repeated-letter row in one call. Nested loops are better for learning; the string fill constructor is a handy shortcut later.
🤔
Did you know?
Each row prints the same letter repeatedly: row 1 prints A once, row 2 prints B twice, row 3 prints C three times. Print the outer loop letter inside the inner loop so the row stays uniform. The reverse twin is Program 10.