V-Shaped Alphabet Pattern in C++

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

What You’ll Learn

This pattern prints letters only on the two diagonals that form a V. Everywhere else, it prints spaces.

The last row prints a single vertex letter (E), because the right diagonal intentionally skips the last letter.

⭐ Pattern Output

Output for 5 letters (width \(2n-1 = 9\)):

Output
A       A
 B     B
  C   C
   D D
    E
1

C++ Program (Reference Logic)

Two scans per row: one left-to-right, one right-to-left (starting from D).

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

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

🧠 How It Works

1

Outer i (65–69)

for (i = 65; i <= 69; i++) walks each row; i is the ASCII code of the letter that should appear on the two legs for that row (and only once at the apex).

Row
2

Left scan j

for (j = 65; j <= 69; j++): if i == j, cout << char(j); else a space. Exactly one column matches per row, forming the downward leg from the left.

Diag
3

Right scan k from 68 down

for (k = 68; k >= 65; k--) uses the same i == k rule. Starting at 68 ('D') skips a second 'E' on the bottom row while still drawing the inward leg.

Mirror
4

Newline & width

After both passes, cout << "\n" ends the row. Each line has a fixed character count (five left slots plus four right), so wide output scrolls horizontally on small screens via the shared layout styles.

Width
=

Converging V

As i increases, the matching columns move toward the center. Five rows × two O(n) inner loops → O(n²) time, O(1) extra space.

2

Variation — User Input

Generalize to any rows (A..endChar). The right scan starts at endChar - 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 = startChar; j <= endChar; ++j) {
            cout << (i == j ? j : ' ');
        }
        for (char k = char(endChar - 1); k >= startChar; --k) {
            cout << (i == k ? k : ' ');
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Double the horizontal gap by printing two spaces instead of one
  • Star version: print * on diagonals using the same equality checks
  • Keep rows within 26 if you want only letters A..Z

Avoid

  • Including endChar in the right scan unless you want a double vertex
  • Using a proportional font when you care about alignment

Key Takeaways

1

Only diagonal positions print letters; everything else is spaces.

2

Right scan is length n - 1 so width is 2n - 1.

3

Bottom row prints a single vertex letter.

4

O(n²) time for n rows.

❓ Frequently Asked Questions

It can, but then the bottom row would print E twice (left and right), changing the intended V-shape.
O(n²) because each row scans across \(2n-1\) positions.

Explore More C++ Alphabet Patterns!

Diagonal patterns are just equality checks between row and column indices.

All Alphabet Patterns →
Did you know?

On each row, at most two cells print letters (one per leg)—except the last row prints only one because the right scan does not include the last letter.

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