Sequential Decreasing Alphabet Triangle in C++

What You’ll Learn
This pattern prints a continuous sequence of letters across rows, but each new row prints one fewer character than the previous.
We use setw(2) so the output matches the spaced layout in the sample (best in a monospace terminal).
⭐ Pattern Output
Output for 5 rows:
A B C D E
F G H I
J K L
M N
OC++ Program (Reference Logic)
One counter k supplies letters; the inner loop just decides how many times to print.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
int k = 65;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= i; j--)
cout << setw(2) << char(k++);
cout << "\n";
}
return 0;
}🧠 How It Works
k walks forward through the alphabet
Initialize k = 65 (or 'A' in the variation). Each letter output uses cout << setw(2) << char(k++) so the triangle is one continuous stream rather than restarting every row.
Outer i picks the row’s low letter
i runs A…E as a character or ASCII code. It is the smallest letter allowed on that row; the inner bound stops the slice from going below i.
Inner j from E down to i
Counting down from the top letter to the row floor yields shorter rows as i rises. The iteration count is 'E' - i + 1 in this sample, giving widths 5,4,3,2,1.
setw(2) for alignment
Width-two output adds leading space before single letters so the right-aligned triangle matches the diagram and stays readable when a long line scrolls sideways on mobile.
Triangular total
5+4+3+2+1 = 15 letters through O. Nested loops over shrinking rows give O(n²) character output for n rows.
Variation — User Input
Read rows, compute endChar, and keep k running forward.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
char k = 'A';
char start = 'A';
char end = char('A' + rows - 1);
for (char i = start; i <= end; ++i) {
for (char j = end; j >= i; --j) {
cout << setw(2) << k++;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Use
setw(3)for wider spacing - Print lowercase by starting
kat'a' - Cap
rowsso output stays in the alphabet range you want
Avoid
- Resetting
keach row (breaks the continuous sequence) - Mixing padding and
setw()widths without checking alignment
Key Takeaways
Keep one running k++ so letters continue across rows.
Row length decreases by 1 each line.
Total letters for n rows is n(n+1)/2.
O(n²) time for n rows.
❓ Frequently Asked Questions
k to any starting character (like 'D') and the stream will continue from there.Explore More C++ Alphabet Patterns!
Patterns where one variable supplies the symbol and another variable controls repetition appear often in interview-style problems.
For n rows, total printed letters are \(n(n+1)/2\). For n=5, that’s 15 letters, ending at O.
12 people found this page helpful
