C++ Sequential Alphabet Pyramid Pattern (Right-Aligned)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned sequential alphabet pyramid prints a continuous letter stream (A, then B C, then D E F, …) into width-2 cells, with empty cells on the left for alignment.

Remember
Rule: pad width-2 cells, then next letters via k++ (never reset)

        A
      B C
    D E F
  G H I J
K L M N O     ← 5 rows (15 letters A–O)

Unlike Program 13 (same running counter, left-aligned), this pattern uses fixed-width cells so the pyramid sits on the right in monospace output.

How to Solve It

Scan a fixed number of width-2 slots per row: print empty cells first, then advance a shared letter counter.

MethodIdeaBest for
Fixed-width scanInner j from n..1; pad if j > i else k++Learning one-loop alignment
Explicit padPrint n - i empty cells, then i lettersClearer reading once the shape is clear

Pseudocode

Pseudocode
k = 'A'
for i from 1 to n:
    for j from n down to 1:
        if j > i:
            print two spaces
        else:
            print k in width 2
            k = next letter
    print newline

Cheat sheet

GoalPattern
Row countfor (int i = 1; i <= n; i++)
Scan slotsfor (int j = n; j >= 1; j--)
Empty cellcout << " "; when j > i
Letter cellcout << setw(2) << k++;
Explicit padfor (int s = 0; s < n - i; s++) cout << " ";
End the rowcout << "\n";
A–Z-safe heightCap so n(n+1)/2 ≤ 26 (max 6 rows)

Printing Letters vs Starting a New Line

APIEffectUse for
cout << " " / setw(2) << k++Stays on the same lineEach empty cell and each letter cell
cout << "\n"Ends the current lineAfter the fixed-width scan

Print cells without a newline, then end the row once. Include <iomanip> for setw.

Live Preview

Change the row count and the sequential pyramid updates instantly — including letter totals and last letter.

Whole numbers from 1 to 6. Total letters = n(n+1)/2 (stays within A–U here).

Live result 5 rows · 15 letters · ends O
        A
      B C
    D E F
  G H I J
K L M N O

Worked Walkthrough — n = 4

Trace each row’s pad count, how many letters print, and where the shared counter lands.

Row iPadsLettersk rangePrinted row
131AA
222B CB C
313D E FD E F
404G H I JG H I J

Total letters: 1 + 2 + 3 + 4 = 10 = n(n+1)/2. Never reset k between rows or the sequence restarts at A.

C++ Programs

Three complete programs: fixed 5 rows, row-count input, and an explicit pad-then-letters form. Use View Output to reveal sample results.

Example 1 — Fixed 5 Rows

A single counter k increments only when a letter is printed, and setw(2) keeps columns aligned.

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

int main() {
    char k = 'A';
    int i, j;

    for (i = 1; i <= 5; i++) {
        for (j = 5; j >= 1; j--) {
            if (j > i)
                cout << "  ";
            else
                cout << setw(2) << k++;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop grows letter count. Row i prints i letters into a 5-slot scan.

2. Pad or letter. While j > i, print " "; otherwise print k++ with setw(2).

3. Counter never resets. When i = 3, letters are D E F; the next row continues at G.

Example 2 — Row Count Input

Read the height at runtime. For large values, letters go past Z — prefer a letter-budget cap in real apps.

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

int main() {
    int n, i, j;
    char k = 'A';

    cout << "Enter number of rows (like 5): ";
    cin >> n;

    for (i = 1; i <= n; i++) {
        for (j = n; j >= 1; j--) {
            if (j > i)
                cout << "  ";
            else
                cout << setw(2) << k++;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Same pad/letter rules. Only the shared width follows n instead of the literal 5.

2. Letter budget. Total letters = n(n+1)/2 — cap so it stays ≤ 26 for A–Z only.

3. Safer input tip. Check cin and keep the triangular count in A–Z:

Safer input
if (!(cin >> n) || n < 1 || n * (n + 1) / 2 > 26) {
    cout << "Enter rows so letters stay in A–Z (max 6).\n";
    return 1;
}

Example 3 — Pad Cells, Then Letters

Often clearer to read: print n - i empty cells, then i sequential letters.

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

int main() {
    int n = 5;
    char k = 'A';
    int i, s, L;

    for (i = 1; i <= n; i++) {
        for (s = 0; s < n - i; s++)
            cout << "  ";

        for (L = 0; L < i; L++)
            cout << setw(2) << k++;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Explicit pad count. Print n - i empty width-2 cells first.

2. Then letters. Print exactly i letters via k++ — same continuous sequence.

3. Same visual. Both still use width-2 cells so the pyramid matches the scan version.

Edge Cases & Pitfalls

Check these before calling the solution done.

Reset k

Wrong letters

Resetting k each row restarts at A every line. Keep one counter outside the outer loop.

One space

Misaligned columns

Padding with a single space breaks width-2 alignment. Empty cells must be " " to match setw(2).

No pads

Left-aligned

Skipping the pad branch left-aligns the sequential rows — you lose the right-aligned pyramid.

n = 1

Single A

Output is just A (no pads) — a good sanity check.

Past Z

Clamp letter budget

More than 6 rows needs more than 26 letters. Cap n so n(n+1)/2 ≤ 26, or define a wrap rule.

cin fail

Check the stream

If cin fails, n may be unset — always test if (!(cin >> n)) and clamp the triangular letter count for A–Z demos.

Time and Space Complexity

ProgramTimeExtra space
Scan / explicit pad formsO(n²)O(1)

Each of n rows scans n width-2 slots (or pads + letters totaling O(n)). Letters printed equal n(n+1)/2.

Key Takeaways

  • Running counter: one k walks the alphabet across every row — do not reset it.
  • Width-2 cells: pad with " " and print letters with setw(2).
  • Right align: empty cells first, then the next i letters.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, pad width-2 cells then print the next letters from a continuous counter.

Frequently Asked Questions

Because k is not reset inside the outer loop. Each time a letter is printed we use k++ so the sequence continues A, then B C, then D E F, and so on.
Letters are printed with setw(2). Two spaces keep empty cells the same width so columns align.
Decide a rule: stop at Z, wrap back to A, or switch to a longer alphabet list. Then apply it when incrementing k.
cout << setw(2) << k++ stays on the same line for each cell. cout << "\n" ends the row after the fixed-width scan finishes.
Program 13 also uses a running letter counter, but prints left-aligned without fixed-width padding. This pattern right-aligns by printing empty 2-column cells first.
O(n²) for n rows when each row scans n slots in the inner loop.
After cin >> n, check cin.fail(), require n ≥ 1, and cap so n(n+1)/2 ≤ 26 if you want only A–Z letters (max 6 rows).
Yes. Print n − i empty width-2 cells, then i letters via k++. Example 3 on this page shows that style.

Did you know?

Each slot is 2 columns wide. Padding uses " " and letters use setw(2) so columns line up in monospace output. The counter k never resets, so letters run continuously from A to O for 5 rows.

Next: Right-Aligned Reverse

Descending letters right-aligned with leading spaces.

Program 23 tutorial →

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