Right-Angled Triangle Alphabet Pattern in C++

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

What You’ll Learn

How to print an alphabet right-angled triangle in C++: each row starts at 'A' (ASCII 65) and extends one letter further than the previous row, using nested for loops and cout with a (char) cast.

With five rows, the output is A, AB, ABC, ABCD, and ABCDE—the same shape as a star triangle, but with letters.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

Output
A
AB
ABC
ABCD
ABCDE
1

Complete C++ Program

Fixed five rows: outer loop sets the last ASCII code on each row (6569); inner loop prints from 'A' up to that code.

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) j;
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Headers and namespace

#include <iostream> brings in cout. using namespace std; lets you write cout without the std:: prefix.

Setup
2

Outer loop (rows)

for (i = 65; i <= 69; i++) runs five times. The value of i is the ASCII code of the last letter on that row (65 = A through 69 = E).

Row control
3

Inner loop (letters)

for (j = 65; j <= i; j++) prints every code from 'A' through i. cout << (char) j; casts the integer to a character for display.

Print
4

New line

After the inner loop, cout << "\n"; moves to the next row. The outer loop then increases i, so the next row prints one more letter.

Next row
=

Right-angled letter triangle

Total characters printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Read the number of rows at runtime with cin:

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

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 65; i < 65 + rows; i++) {
        for (j = 65; j <= i; j++)
            cout << (char) j;
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Use static_cast<char>(j) instead of (char)j for clearer C++ style
  • Replace 65 with 'A' in loop bounds for readability
  • Print lowercase by starting from 'a' (ASCII 97)
  • Add spaces between letters with an extra cout << ' ' inside the inner loop

Avoid

  • Printing j without a cast—cout would show integers, not letters
  • Forgetting the newline after each row
  • Using j <= 69 in the inner loop instead of j <= i (that would print a full A–E block on every row)
  • Ignoring invalid input when using cin for rows

Key Takeaways

1

Row k (1-based) prints k letters from 'A' upward because the inner loop runs from 65 to the current outer i.

2

ASCII codes line up with characters: 65 is 'A', 66 is 'B', and so on.

3

Complexity is O(n²) for n rows because the inner loop grows with the row index.

4

The same nested-loop idea applies to C++ star patterns—only what you print changes.

❓ Frequently Asked Questions

They are equivalent for this pattern: char promotes to int in expressions. Looping with int and casting to (char) for output is a common teaching style. You could also write for (char c = 'A'; c <= i; c++) with a char outer i.
The inner loop always initializes j to 65. Only the upper bound i changes, so every row begins at 'A' and grows wider.
O(n²) for n rows: you print 1+2+…+n = n(n+1)/2 characters.
Yes. Use the second example: after reading rows, run the outer loop while i < 65 + rows so the pattern has the right height.

Explore More C++ Alphabet Patterns!

Letter-based triangles and pyramids build on the same loop ideas you used here.

All Alphabet Patterns →
Did you know?

In C++, char is an integer type: 'A' + 1 has value 'B'. That is why looping with ASCII codes and casting with (char) matches what you would do with char variables.

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