Decreasing & Increasing Alphabet Rows in C++

What You’ll Learn
Each row is made from two parts: a descending run from the row letter down to B, then an ascending run starting from A up to a computed cap so every row stays the same width.
The key formula is cap = startChar + endChar - i (which becomes 134 - i in the ASCII version for A.. E).
⭐ Pattern Output
Output for A.. E (no spaces):
ABCDE
BABCD
CBABC
DCBAB
EDCBAC++ Program (Reference Logic, ASCII bound)
This uses the same bound as the reference: k <= 134 - i.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
for (i = 65; i <= 69; i++) {
for (j = i; j > 65; j--)
cout << char(j);
for (k = 65; k <= 134 - i; k++)
cout << char(k);
cout << "\n";
}
return 0;
}🧠 How It Works
Outer i is the row anchor (ASCII)
i runs 65…69 (A…E). It is the leftmost high letter on the row; the second loop prints the ascending tail that keeps every line five characters wide.
Descending leg: j from i toward B
for (j = i; j > 65; --j) emits char(j) with cout. When i == 65 the condition fails immediately, so row one is only the ascending half.
Ascending leg: k through 134 - i
for (k = 65; k <= 134 - i; ++k) is 65 + 69 - i in ASCII terms ('A' + 'E' - i). As i increases, the cap drops so the combined segments always print exactly five letters for this sample range.
Constant row length
Descending count is i - 65; ascending count is (134 - i) - 65 + 1 = 69 - i + 1. Their sum is 5 for every row.
Bow-tie pattern
Middle rows splice a descending prefix with an ascending tail. Two inner passes per row → O(n²) for n letters in the span.
Variation — User Input (no magic numbers)
Generalize using startChar and endChar so the cap becomes startChar + endChar - i.
#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 = i; j > startChar; --j) cout << j;
for (char k = startChar; k <= char(startChar + endChar - i); ++k) cout << k;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Print a space after each character for readability
- Derive the cap as
endChar - (i - startChar)(equivalent form) - Validate
rowsto stay within A..Z
Avoid
- Using
2 * endChar - i(overshoots on the first row) - Letting the descending loop print
A(duplicates A at the join)
Key Takeaways
Cap formula: startChar + endChar - i (ASCII: 134 - i for A..E).
Descending loop stops before A to avoid duplicates.
Row length remains constant: endChar - startChar + 1.
O(n²) output for span n.
❓ Frequently Asked Questions
134 = 65 + 69 which is 'A' + 'E'. So 134 - i is just 'A' + 'E' - i.A is printed by the second loop. This prevents printing A twice.Explore More C++ Alphabet Patterns!
Formulas like start + end - i are a handy way to avoid hard-coded bounds when generating symmetric strings.
The second loop prints (A + E - i) - A + 1 = E - i + 1 letters, which perfectly complements the i - A letters from the first loop to keep every row length constant.
12 people found this page helpful
