A reverse alphabet diagonal-star pattern prints the same reverse letter run on every row (for example EDCBA), then swaps exactly one cell for * where the row key equals the column letter.
Remember
Rule: for row key i = A..top,
print top..A, but '*' when i == j
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA ← top = 'E'
Because columns run E..A while rows run A..E, the match slides right → left. Flip the inner loop to A..E and the star slides the other way.
Approach
How to Solve It
Nested char loops plus one equality test — the same idea works for reverse or forward column order.
Method
Idea
Best for
Reverse columns
Inner j = top..A; star slides right → left
Learning, interviews, exams
Forward columns
Inner j = A..top; star slides left → right
Comparing direction variants
Pseudocode
Pseudocode
top = 'E'
for i from 'A' to top:
for j from top down to 'A':
if i == j:
print '*'
else:
print j
print newline
Cheat sheet
Goal
Pattern
Pick the row key
for (char i = 'A'; i <= top; i++)
Scan reverse letters
for (char j = top; j >= 'A'; j--)
Star or letter
cout << (i == j ? '*' : j);
End the row
cout << "\n";
Grid size
n = top - 'A' + 1 rows and columns
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << '*'
One star; stays on the line
Diagonal cell when i == j
cout << j
One letter; stays on the line
Every other cell
cout << "\n"
Ends the current line
After the column loop
cout << endl also ends the line (and flushes); "\n" is enough for these demos.
Try it
Live Preview
Change the top letter and the diagonal-star grid updates instantly — capped at H for readability.
Single uppercase letter from A to H. With E you get the classic sample.
Live result5×5 · 25 cells
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
Trace
Worked Walkthrough — top = 'C'
Smaller board (3×3) so each i == j match is easy to check by hand.
Row key i
Columns j (C..A)
Printed row
'A'
C, B, *
CB*
'B'
C, *, A
C*A
'C'
*, B, A
*BA
The star starts on the right and moves one column left each row.
Code
C++ Programs
Three complete programs: fixed top letter, cin input, and a forward-column variant. Use View Output to reveal sample results.
Example 1 — Fixed top at 'E'
Outer i runs A..E. Inner j runs E..A. When i == j, print *; otherwise print j.
C++
#include <iostream>
using namespace std;
int main()
{
char i, j;
for (i = 'A'; i <= 'E'; i++)
{
for (j = 'E'; j >= 'A'; j--)
{
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}
Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
How It Works
1. Outer loop picks the key.i runs from 'A' to 'E' — one row per key letter.
2. Inner loop prints reverse letters.j walks from 'E' down to 'A' on every row.
3. Swap one cell for a star. When i == j, print * instead of that letter.
4. Break the line.cout << "\n" after the column loop — the star slides one step left each row.
Example 2 — User Input Version
Read the top letter with cin. Prefer validating a single A–Z character in real apps.
C++
#include <iostream>
using namespace std;
int main()
{
char top, i, j;
cout << "Enter the top letter (like E): ";
cin >> top;
for (i = 'A'; i <= top; i++)
{
for (j = top; j >= 'A'; j--)
cout << (i == j ? '*' : j);
cout << "\n";
}
return 0;
}
Output (when user enters D)
Enter the top letter (like E): D
DCB*
DC*A
D*BA
*CBA
How It Works
1. Prompt and read.cin >> top sets both loop bounds.
2. Same diagonal core. Only top changes; i == j still places one star per row.
3. Ternary shortcut.cout << (i == j ? '*' : j) is the same branch as Example 1 in one expression.
4. Safer input tip. Reject lowercase or non-letters:
Safer input
if (!(cin >> top) || top < 'A' || top > 'Z')
{
cout << "Enter an uppercase letter A–Z.\n";
return 1;
}
Example 3 — Forward A..E with diagonal *
Flip the inner loop to A..E. The star now slides left → right on the main diagonal.
C++
#include <iostream>
using namespace std;
int main()
{
char i, j;
for (i = 'A'; i <= 'E'; i++)
{
for (j = 'A'; j <= 'E'; j++)
cout << (i == j ? '*' : j);
cout << "\n";
}
return 0;
}
Output
*BCDE
A*CDE
AB*DE
ABC*E
ABCD*
How It Works
1. Same diagonal test.i == j still marks one cell per row.
2. Only column order flips. Letters now run A..E, so the star starts on the left.
3. Compare with Example 1. Inner-loop direction controls both letter order and which way the star travels.
Edge Cases & Pitfalls
Check these before calling the solution done.
Wrong compare
No star / many stars
Use i == j (exact match). Off-by-one compares place the star on the wrong column.
Inner direction
Unexpected slide
Forward A..top vs reverse top..A flips both letter order and star travel direction.
\n early
Broken rows
Call cout << "\n" only after the column loop finishes.
top = 'A'
Single star
Output is just * on one line — one cell, one match.
cin
Check failure
Use if (!(cin >> top)) and require a single A–Z letter.
Spaces
Compact grid
This pattern prints letters with no separators. Adding spaces changes the visual diagonal alignment.
Analysis
Time and Space Complexity
Program
Time
Extra space
Reverse (Examples 1–2)
O(n²)
O(1)
Forward (Example 3)
O(n²)
O(1)
For n = top - 'A' + 1 there are n rows and each row prints n cells — quadratic in n.
Remember
Key Takeaways
Rule: print top..A, but * wherever i == j.
One star per row: the equality match lands on exactly one column.
Direction matters: reverse columns slide the star left; forward slides it right.
Complexity:O(n²) time; O(1) extra space.
One line: for each row key i, print top..A with * where i == j, then cout << "\n".
Frequently Asked Questions
i walks A..E down the rows while j scans E..A across columns. The condition i == j marks one diagonal position per row, which gets replaced by '*'.
Because i increases each row, but the printed letters go from E down to A across the row. The match i==j occurs at a different column each time, sliding the star left.
The star prints when i == j, so each row replaces exactly one character at the matching column.
cout << ch stays on the same line for each cell. cout << "\n" ends the row after the inner loop finishes.
Yes. Replace '*' with any symbol (like '#' or '@') in the conditional branch.
You print forward letters instead of E..A, and the star slides the other way (left to right on the main diagonal).
O(n²) for an n×n letter grid because every row prints n characters.
Use cin >> top, require A–Z, and reject failed input. Cap at Z if you only want alphabetic ranges.
🤔
Did you know?
The diagonal is defined by i == j while the row prints letters from E down to A. Since i increases A..E each row, the star moves one position left each line: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.