Reverse Alphabet, Diagonal *

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
EDCB*
EDC*A
ED*BA
E*CBA
*DCBAComplete 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.
#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
Outer i
Row index A through E; also the value that will match j on the diagonal.
Inner j descends
Because j runs E down to A, each row would print EDCBA without the star condition.
if (i == j)
On the matching cell, print * instead of the letter.
Row 1 walkthrough
i = 'A': j prints E,D,C,B; when j reaches A, it equals i and becomes the star → EDCB*.
Square grid
5 × 5 cells → 25 prints → O(n²) for n letters.
Variation — User Input
endChar = 'A' + rows - 1; loops use startChar and endChar.
#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
ABCDEwith a diagonal hole - Cap
rowssoendChar <= 'Z'
Avoid
- Using
rowslarger than 26 without changing the alphabet range - Forgetting the reverse
jloop (you would print ABCDE instead of EDCBA)
Key Takeaways
Descending j prints reverse letters on each line.
i == j picks one diagonal cell per row.
Generalizes by setting endChar from rows.
O(n²) for an n×n letter square.
❓ Frequently Asked Questions
j counts down), the stars sit on one anti-diagonal of the letter matrix.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.Explore More C++ Alphabet Patterns!
Pairing indices with character codes is a compact way to mark one cell per row.
In C++ comparisons, char values are promoted to integers, so comparing ASCII-based i and j works reliably for standard letter ranges.
12 people found this page helpful
