Repeating-Letter Alphabet Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
A
BB
CCC
DDDD
EEEEE
1

Complete C++ Program

Outer i from 65 to 69. Inner j from 65 to i; each time print (char) i (not j).

C++
#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

1

Headers and namespace

#include <iostream> and using namespace std; enable cout.

Setup
2

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.

Row control
3

Inner loop: repeat i

j from 65 to i gives i - 65 + 1 iterations. cout << (char) i prints that row’s letter every time.

Repeat
4

New line

cout << "\n" ends the row; then i++ moves to the next letter.

Next row
=

Triangular row lengths

Row r prints r letters, so total characters are 1+2+…+nO(n²) output for n rows.

2

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.

C++
#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, inner j 1..row printing ch, then ch++ after each row
  • Start at base = 97 for lowercase repeats
  • Compare with C++ star pattern 1 (same geometry, * instead of letters)

Avoid

  • cout << (char) j here—that would print AB, ABC, … like program 1, not repeats
  • Unbounded rows without thinking about passing 'Z'
  • Incrementing i inside the inner loop

Key Takeaways

1

Inner loop counts iterations; the printed character is always (char) i for that row.

2

Outer i++ advances the letter between rows, like ch++ after each row in the char-variable version.

3

O(n²) prints for n rows.

4

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.
So the number of iterations is i - 65 + 1. You could use j = 1; j <= i - 64 instead with the same effect.
Yes: set endCode = 65 + rows - 1. After 'Z' you leave the usual alphabet unless you add wrapping or a cap.
O(n²) for n rows: n(n+1)/2 characters.

Explore More C++ Alphabet Patterns!

Repeating letters vs stepping letters is a one-line change in what you print inside the inner loop.

All Alphabet Patterns →
Did you know?

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.

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