Repeating-Letter Alphabet Triangle in C++

What You’ll Learn
Row 1 is one A, row 2 is two Bs, row 3 three Cs, and so on: A, BB, CCC, DDDD, EEEEE.
The nested loop shape matches program 1, but each inner iteration prints the same character (char) i instead of stepping through the alphabet inside the row.
⭐ Pattern Output
For 5 rows:
A
BB
CCC
DDDD
EEEEEComplete C++ Program
Outer i from 65 to 69. Inner j from 65 to i; each time print (char) i (not j).
#include <iostream>
using namespace std;
int main() {
int i, j;
for (i = 65; i <= 69; i++) {
for (j = 65; j <= i; j++)
cout << (char) i;
cout << "\n";
}
return 0;
}🧠 How It Works
Headers and namespace
#include <iostream> and using namespace std; enable cout.
Outer loop: row letter and width
i is the ASCII code for the letter on this row (A through E). It also sets how many inner iterations run.
Inner loop: repeat i
j from 65 to i gives i - 65 + 1 iterations. cout << (char) i prints that row’s letter every time.
New line
cout << "\n" ends the row; then i++ moves to the next letter.
Triangular row lengths
Row r prints r letters, so total characters are 1+2+…+n — O(n²) output for n rows.
Variation — User Input Version
Let base = 65 and endCode = base + rows - 1. Outer i runs from base to endCode. Cap rows or handle letters past 'Z' if you generalize.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
const int base = 65;
cout << "Enter the number of rows: ";
cin >> rows;
int endCode = base + rows - 1;
for (i = base; i <= endCode; i++) {
for (j = base; j <= i; j++)
cout << (char) i;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Clearer inner counter:
for (int k = 1; k <= i - base + 1; k++)and still print(char) i - Same output with a
char ch = 'A', outer row index 1..n, innerj1..row printingch, thench++after each row - Start at
base = 97for lowercase repeats - Compare with C++ star pattern 1 (same geometry,
*instead of letters)
Avoid
cout << (char) jhere—that would print AB, ABC, … like program 1, not repeats- Unbounded
rowswithout thinking about passing'Z' - Incrementing
iinside the inner loop
Key Takeaways
Inner loop counts iterations; the printed character is always (char) i for that row.
Outer i++ advances the letter between rows, like ch++ after each row in the char-variable version.
O(n²) prints for n rows.
Good bridge between star triangles and A, AB, ABC patterns.
❓ Frequently Asked Questions
j is only there to run the inner loop the right number of times. The letter you want on the whole row is i.i - 65 + 1. You could use j = 1; j <= i - 64 instead with the same effect.endCode = 65 + rows - 1. After 'Z' you leave the usual alphabet unless you add wrapping or a cap.Explore More C++ Alphabet Patterns!
Repeating letters vs stepping letters is a one-line change in what you print inside the inner loop.
The reference inner loop could be written as for (j = 1; j <= i - 64; j++) to make the “repeat count” obvious; using j from 65 to i ties the count to ASCII codes instead.
12 people found this page helpful
