Inverted V-Shaped Alphabet Pattern in C++

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

What You’ll Learn

This pattern draws an inverted V: one A at the top, then pairs like B B, C C, and so on, widening as you go down.

We scan a fixed-width row and print letters only where the row letter matches the current column index.

⭐ Pattern Output

Output for 5 rows:

Output
    A
   B B
  C   C
 D     D
E       E
1

C++ Program (Reference Logic)

Left scan from E to A, then right scan from B to E.

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

int main() {
    int i, j, k;
    for (i = 65; i <= 69; i++) {
        for (j = 69; j >= 65; j--) {
            if (i == j) cout << char(j);
            else cout << " ";
        }
        for (k = 66; k <= 69; k++) {
            if (i == k) cout << char(k);
            else cout << " ";
        }
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Row letter i

for (char i = 'A'; i <= 'E'; ++i) walks each row. The current letter is the one that may appear on both legs (except the apex row, which only prints once).

Row
2

Left block j (E..A)

for (char j = 'E'; j >= 'A'; --j) with cout << (i == j ? j : ' ') draws the descending diagonal: only the column where j matches i shows a letter; the rest are spaces.

Left
3

Right block k (B..E)

for (char k = 'B'; k <= 'E'; ++k) mirrors the rule on the ascending side. Starting at B skips a duplicate A on the first row while still forming the widening V.

Right
4

Symmetry & newline

Both inner loops use the same predicate (i == column) so legs stay aligned. cout << "\n" after each pair finishes the row before the next i.

Align
=

Opening downward

Each row adds width between the legs. Five rows with two O(n) passes per row → O(n²) time and O(1) extra space beyond the stream.

2

Variation — User Input

For rows, set endChar = 'A' + rows - 1 and run the same two scans; the right scan starts at startChar + 1.

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

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

    char startChar = 'A';
    char endChar = char('A' + rows - 1);

    for (char i = startChar; i <= endChar; ++i) {
        for (char j = endChar; j >= startChar; --j) {
            cout << (i == j ? j : ' ');
        }
        for (char k = char(startChar + 1); k <= endChar; ++k) {
            cout << (i == k ? k : ' ');
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Stretch the shape by printing two spaces instead of one
  • Fill the inside region for a solid triangle
  • Keep rows <= 26 for A..Z only

Avoid

  • Starting the right scan at A if you want a single apex
  • Proportional fonts when evaluating alignment

Key Takeaways

1

Two diagonal checks pick out the two legs.

2

Right block starts at B so the first row prints only one A.

3

Width is 2n - 1 for n letters.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

So row A doesn’t print twice. From row B onward, both legs can match the same letter.
O(n²) because each row scans O(n) columns and there are n rows.

Explore More C++ Alphabet Patterns!

Once you can pick diagonal positions with equality checks, you can draw many shapes in a console grid.

All Alphabet Patterns →
Did you know?

If the right block started at A, the top row would print two As (one on each side) unless you changed the ranges.

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