Right-Angled Triangle Alphabet Pattern in C++

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:
A
AB
ABC
ABCD
ABCDEComplete C++ Program
Fixed five rows: outer loop sets the last ASCII code on each row (65–69); inner loop prints from 'A' up to that code.
#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
Headers and namespace
#include <iostream> brings in cout. using namespace std; lets you write cout without the std:: prefix.
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).
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.
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.
Right-angled letter triangle
Total characters printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read the number of rows at runtime with cin:
#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)jfor clearer C++ style - Replace
65with'A'in loop bounds for readability - Print lowercase by starting from
'a'(ASCII97) - Add spaces between letters with an extra
cout << ' 'inside the inner loop
Avoid
- Printing
jwithout a cast—coutwould show integers, not letters - Forgetting the newline after each row
- Using
j <= 69in the inner loop instead ofj <= i(that would print a full A–E block on every row) - Ignoring invalid input when using
cinforrows
Key Takeaways
Row k (1-based) prints k letters from 'A' upward because the inner loop runs from 65 to the current outer i.
ASCII codes line up with characters: 65 is 'A', 66 is 'B', and so on.
Complexity is O(n²) for n rows because the inner loop grows with the row index.
The same nested-loop idea applies to C++ star patterns—only what you print changes.
❓ Frequently Asked Questions
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.j to 65. Only the upper bound i changes, so every row begins at 'A' and grows wider.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.
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.
12 people found this page helpful
