Sequential Decreasing Alphabet Triangle in C++

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

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:

Output
A B C D E
F G H I
J K L
M N
O
1

C++ Program (Reference Logic)

One counter k supplies letters; the inner loop just decides how many times to print.

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

1

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.

Stream
2

Outer i picks the row’s low letter

i runs AE 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.

Rows
3

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.

Count
4

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.

Align
=

Triangular total

5+4+3+2+1 = 15 letters through O. Nested loops over shrinking rows give O(n²) character output for n rows.

2

Variation — User Input

Read rows, compute endChar, and keep k running forward.

C++
#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 k at 'a'
  • Cap rows so output stays in the alphabet range you want

Avoid

  • Resetting k each row (breaks the continuous sequence)
  • Mixing padding and setw() widths without checking alignment

Key Takeaways

1

Keep one running k++ so letters continue across rows.

2

Row length decreases by 1 each line.

3

Total letters for n rows is n(n+1)/2.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

Because the pattern is meant to continue A, B, C... across the whole triangle.
Yes—initialize k to any starting character (like 'D') and the stream will continue from there.
O(n²) because total printed letters are triangular in n.

Explore More C++ Alphabet Patterns!

Patterns where one variable supplies the symbol and another variable controls repetition appear often in interview-style problems.

All Alphabet Patterns →
Did you know?

For n rows, total printed letters are \(n(n+1)/2\). For n=5, that’s 15 letters, ending at O.

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