Decreasing and increasing alphabet rows keep a fixed width: each row starts with a growing descending prefix, then fills the rest with a shrinking ascending suffix from A.
As the left side grows (B, CB, DCB, …), the right side shrinks (ABCD, ABC, AB, A) so every line stays the same length. Compare with Program 24 (palindrome triangles) and Program 29 (layered diamond).
Approach
How to Solve It
Two ways to emit the same grid — inline two loops per row, or factor a printRow helper.
Method
Idea
Best for
Two loops inline
Descending i..1, then ascending 0..(n−i)
Learning, interviews, exams
printRow helper
One function owns both parts; outer loop only picks i
Cleaner demos once the width budget clicks
Pseudocode
Pseudocode
n = end - 'A'
for i from 0 to n:
for j from i down to 1: print alpha[j]
for k from 0 to n - i: print alpha[k]
print newline
Cheat sheet
Goal
Pattern
End index
int n = end - 'A'; (4 for E)
Rows
for (int i = 0; i <= n; i++)
Descending prefix
for (int j = i; j > 0; j--) cout << alpha[j];
Ascending suffix
for (int k = 0; k <= n - i; k++) cout << alpha[k];
Works for A..end with the same two-part row. Prefer validating a single A–Z character in real apps.
C++
#include <iostream>
using namespace std;
int main() {
char end;
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n, i, j, k;
cout << "Enter end letter (like E): ";
cin >> end;
n = end - 'A';
for (i = 0; i <= n; i++) {
for (j = i; j > 0; j--)
cout << alpha[j];
for (k = 0; k <= n - i; k++)
cout << alpha[k];
cout << "\n";
}
return 0;
}
Output (when user enters C)
Enter end letter (like E): C
ABC
BAB
CBA
How It Works
1. Scale with n.n = end - 'A' drives both loops. For end = C, width is 3 and you get three rows.
2. Same core. Only the end index changes; the prefix/suffix width budget is unchanged.
3. Safer input tip. Check the stream and require A–Z:
Safer input
if (!(cin >> end) || end < 'A' || end > 'Z') {
cout << "Enter one letter from A to Z.\n";
return 1;
}
Example 3 — Helper Function
Often clearer: one function owns both parts so main only walks row indexes.
C++
#include <iostream>
using namespace std;
void printRow(char alpha[], int n, int i) {
int j, k;
for (j = i; j > 0; j--)
cout << alpha[j];
for (k = 0; k <= n - i; k++)
cout << alpha[k];
cout << "\n";
}
int main() {
int n = 4;
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i = 0; i <= n; i++)
printRow(alpha, n, i);
return 0;
}
Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
How It Works
1. printRow owns both parts. Prefix and suffix live in one place.
2. Thin outer loop.main only decides which row index i to print.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 0
Duplicate A at the join
If the prefix includes index 0, you get …AA…. Keep for (j = i; j > 0; j--).
Wrong suffix end
Uneven row widths
Suffix must stop at n − i. Ending at a fixed n makes later rows too long.
\n inside
Column of letters
If cout << "\n" sits inside either loop, each letter lands on its own line. Print letters without a newline; end the row only after both parts.
end = A
Single A
When n = 0, you print one row: A. A good sanity check.
i = 0
Empty prefix is normal
The first row prints only the ascending suffix A..end — that is expected, not a bug.
cin fail
Check the stream
If cin fails, end may be unset — always test if (!(cin >> end)) and require A–Z.
Analysis
Time and Space Complexity
Program
Time
Extra space
Two loops inline (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Helper function (Example 3)
O(n²)
O(1)
For letters A..end there are n + 1 rows, each of width n + 1 — quadratic in n.
Remember
Key Takeaways
Rule: prefix i..1 + suffix 0..(n − i).
Width budget: prefix grows as suffix shrinks — total always n + 1.
Break the row:cout << "\n" only after both loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each i, print i down to B, then A up through index n − i.
Frequently Asked Questions
A is printed in the second loop. If the first loop included A, the join would duplicate A.
To keep each row the same width, end the ascending part at indices 0..(n − i). For A..E that is A + E − i, so both parts always add up to n+1 letters.
Yes. Set n = end − 'A' and keep the same loops: descending j from i down to 1, then ascending k from 0 to n − i.
O(n²) for n letters because there are n rows and each row prints O(n) characters.
The descending prefix has length i and the ascending suffix has length (n − i + 1). Together they always equal n + 1.
Use cin >> end, require A–Z (or a–z), and reject failed input. Cap at Z if you only want alphabetic ranges.
Program 24 builds palindrome triangles around A. This page keeps fixed-width rows by trading a growing descending prefix against a shrinking ascending suffix.
The descending loop does nothing, so you print only the ascending suffix A..end — for E that is ABCDE.
🤔
Did you know?
For each row i (A..E), print a descending prefix from i down to B (skip A), then print an ascending suffix from A up to A + E - i. That cap keeps row width constant at E - A + 1.