Reverse Alphabet, Diagonal *

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

What You’ll Learn

Five rows, each like EDCBA but with exactly one * where the row letter meets the column letter (i == j).

The star slides from the right (EDCB*) to the left (*DCBA) as i increases. Compare program 16 (centered pyramid, no diagonal).

⭐ Pattern Output

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Complete C++ Program (Reference Logic)

Outer i runs from 65 to 69 (A to E). Inner j runs from 69 down to 65 to print reverse letters across the row.

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

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

🧠 How It Works

1

Outer i

Row index A through E; also the value that will match j on the diagonal.

Rows
2

Inner j descends

Because j runs E down to A, each row would print EDCBA without the star condition.

EDCBA
3

if (i == j)

On the matching cell, print * instead of the letter.

Star
4

Row 1 walkthrough

i = 'A': j prints E,D,C,B; when j reaches A, it equals i and becomes the star → EDCB*.

Example
=

Square grid

5 × 5 cells → 25 prints → O(n²) for n letters.

2

Variation — User Input

endChar = 'A' + rows - 1; loops use startChar and endChar.

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

int main() {
    int rows;
    int i, j;
    char startChar, endChar;

    cout << "Enter the number of rows: ";
    cin >> rows;

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

    for (i = startChar; i <= endChar; ++i) {
        for (j = endChar; j >= startChar; --j) {
            if (i == j) cout << "*";
            else cout << (char) j;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Swap * for # or another marker
  • Mirror the inner loop to get ABCDE with a diagonal hole
  • Cap rows so endChar <= 'Z'

Avoid

  • Using rows larger than 26 without changing the alphabet range
  • Forgetting the reverse j loop (you would print ABCDE instead of EDCBA)

Key Takeaways

1

Descending j prints reverse letters on each line.

2

i == j picks one diagonal cell per row.

3

Generalizes by setting endChar from rows.

4

O(n²) for an n×n letter square.

❓ Frequently Asked Questions

In the printed grid (rows top-to-bottom, columns left-to-right as j counts down), the stars sit on one anti-diagonal of the letter matrix.
Use a relation like i + j == startChar + endChar so the match moves along the opposite slant (adjust the range for your letters).
for (i = 65; i <= 69; i++) is the same as for (i = 'A'; i <= 'E'; i++). Literals are easier to read.
O(n²) for n rows when each row has n columns.

Explore More C++ Alphabet Patterns!

Pairing indices with character codes is a compact way to mark one cell per row.

All Alphabet Patterns →
Did you know?

In C++ comparisons, char values are promoted to integers, so comparing ASCII-based i and j works reliably for standard letter 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