Right-Aligned Sequential Pyramid in C++

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

What You’ll Learn

This pattern prints letters in a running sequence (A, then B C, then D E F…) but keeps the triangle right-aligned by printing empty cells first.

For best alignment, view output in a monospace terminal because we rely on fixed-width cells.

⭐ 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 (Fixed 5 Rows)

Direct adaptation of the reference logic using ASCII values and setw(2) for aligned columns.

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 >= 65; j--) {
            if (j > i) cout << "  ";
            else cout << setw(2) << char(k++);
        }
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

k streams the alphabet

k starts at 65 (A) and only advances when a letter is actually printed via cout << setw(2) << char(k++), so the triangle reads as one continuous sequence laid into a fixed grid.

Counter
2

Outer i sets how many letters appear

i walks row codes 6569. Row i exposes i - 64 letter slots; the remaining columns on the line stay padded.

Rows
3

Inner j scans columns high → low

For each j from 69 down to 65, if j > i print two spaces; else print the next streamed letter in a width-two field. <iomanip> setw(2) keeps columns aligned on small screens when the line scrolls horizontally.

Alignment
4

Why the inner loop is always five wide

You always iterate across the full code range for the pattern, so each row has five logical columns. Padding plus setw(2) preserves the staircase shape regardless of viewport width.

Grid
=

Letter count

1+2+3+4+5 = 15 letters through O. Padding adds more writes; time still scales as O(n²) for n rows in this fixed-grid style.

2

Variation — User Input

Choose the number of rows and compute the last character for both loops.

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

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    const int base = 65;
    int last = base + rows - 1;
    int k = base;

    for (int i = base; i <= last; ++i) {
        for (int j = last; j >= base; --j) {
            if (j > i) cout << "  ";
            else cout << setw(2) << char(k++);
        }
        cout << "\n";
    }
    return 0;
}

💡 Tips for Enhancement

Try These

  • Switch to char loops ('A'..)
  • Add a space after each printed letter for readability
  • Validate input so rows stays in 1..26

Avoid

  • Changing padding width without updating setw() (misalignment)
  • Resetting k each row (sequence breaks)

Key Takeaways

1

Use one running counter (k++) to print sequential letters.

2

Keep padding width equal to the letter cell width (setw(2)).

3

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

4

Overall complexity is O(n²).

❓ Frequently Asked Questions

It sets the minimum width for the next insertion into cout, padding with spaces so the output occupies at least 2 characters.
Yes—for example cout << ' ' << char(k++)—but then padding must also be one cell wide to keep alignment consistent.
Alignment depends on equal character widths. In proportional fonts, spaces and letters have different widths so columns won’t line up.
The output will go beyond Z (ASCII) unless you limit rows to 26 or change the character set.

Explore More C++ Alphabet Patterns!

Right alignment is easiest when you treat each printed item as a fixed-width cell.

All Alphabet Patterns →
Did you know?

The total number of printed letters after n rows is the nth triangular number: \(1 + 2 + \dots + n = n(n+1)/2\).

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