Decreasing & increasing alphabet rows keep a fixed width: each row grows a descending prefix and shrinks an ascending suffix so the letter count stays constant.
Remember
Rule: for row i = 0..n,
print alpha[i]..alpha[1], then alpha[0]..alpha[n-i]
ABCDE
BABCD
CBABC
DCBAB
EDCBA ← end = 'E', n = 4
Index i is the “pivot” into alpha[]. The first loop never prints A (j > 0) because the second loop starts at A.
Approach
How to Solve It
Build each row in two parts — descending prefix, then ascending suffix — so width stays n + 1.
Method
Idea
Best for
Inline two parts
Prefix j = i..1, suffix k = 0..n-i
Learning, interviews, exams
Helper print_row
One function owns both parts; main only walks i
Cleaner demos once the parts click
Pseudocode
Pseudocode
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
n = end - 'A' // 4 when end is 'E'
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
Walk each row
for (i = 0; i <= n; i++)
Descending prefix
for (j = i; j > 0; j--)
Ascending suffix
for (k = 0; k <= n - i; k++)
Print a letter
printf("%c", alpha[j]);
End the row
printf("\n");
Row width
n + 1 letters (always)
Printing Letters vs Starting a New Line
API
Effect
Use for
printf("%c", ch)
One letter; stays on the line
Each letter in either part
printf("\n")
Ends the current line
After both parts finish
Try it
Live Preview
Change the letter count (end = A + n - 1) and the rows update instantly — capped at 8 for readability.
Whole numbers from 1 to 8. With n = 5, end is E and you get the classic sample.
Live result5 rows · 25 letters
ABCDE
BABCD
CBABC
DCBAB
EDCBA
Trace
Worked Walkthrough — end = 'C'
Smaller board (n = 2, width 3) so both parts are easy to check by hand.
Row i
Prefix (i..1)
Suffix (0..n-i)
Printed row
0
(empty)
ABC
ABC
1
B
AB
BAB
2
CB
A
CBA
Prefix length i plus suffix length n - i + 1 is always 3 here.
Code
C Programs
Three complete programs: fixed A–E, scanf end letter, and a print_row helper. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Hard-coded n = 4 — descending prefix, then ascending suffix on every row.
C
#include <stdio.h>
int main(void)
{
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i, j, k;
for (i = 0; i <= 4; i++)
{
for (j = i; j > 0; j--)
printf("%c", alpha[j]);
for (k = 0; k <= 4 - i; k++)
printf("%c", alpha[k]);
printf("\n");
}
return 0;
}
Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
How It Works
1. Outer loop picks the pivot.i runs from 0 to 4 — one row per index into alpha.
2. Prefix descends toward B.j runs from i down to 1, so A is never printed here.
3. Suffix ascends from A.k runs from 0 to 4 - i, which shrinks as the prefix grows.
4. Break the line.printf("\n") after both parts — when i = 2 you get CB + ABC → CBABC.
Example 2 — User Input Version
Read the end letter at runtime with scanf. Prefer a leading space in the format and validate A–Z.
C
#include <stdio.h>
int main(void)
{
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char end;
int n, i, j, k;
printf("Enter end letter (like E): ");
scanf(" %c", &end);
n = end - 'A';
for (i = 0; i <= n; i++)
{
for (j = i; j > 0; j--)
printf("%c", alpha[j]);
for (k = 0; k <= n - i; k++)
printf("%c", alpha[k]);
printf("\n");
}
return 0;
}
Output (when user enters C)
Enter end letter (like E): C
ABC
BAB
CBA
How It Works
1. Prompt and read.scanf(" %c", &end) — the leading space skips leftover newlines.
2. Scale with n.n = end - 'A' sets both loop bounds; for C, width is 3.
3. Same two-part core. Only n changes; prefix and suffix rules stay identical.
4. Safer input tip. Reject lowercase or non-letters:
Safer input
if (scanf(" %c", &end) != 1 || end < 'A' || end > 'Z')
{
printf("Enter an uppercase letter A–Z.\n");
return 1;
}
Example 3 — Helper function
Same shape with print_row so main only walks row indexes.
C
#include <stdio.h>
void print_row(char alpha[], int n, int i)
{
int j, k;
for (j = i; j > 0; j--)
printf("%c", alpha[j]);
for (k = 0; k <= n - i; k++)
printf("%c", alpha[k]);
printf("\n");
}
int main(void)
{
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n = 4;
int i;
for (i = 0; i <= n; i++)
print_row(alpha, n, i);
return 0;
}
Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
How It Works
1. Centralize both parts.print_row owns the prefix, suffix, and newline.
2. Keep main thin. It only loops i from 0 to n.
3. Learn the inline version first. Use Examples 1–2 when you need every branch visible; treat the helper as a polish shortcut.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 0
Duplicated A
If the prefix loop includes 0, A prints twice. Keep j > 0.
Wrong bound
Uneven rows
Suffix must stop at n - i. A fixed end letter breaks the constant width.
\n early
Broken rows
Call printf("\n") only after both parts finish.
end = 'A'
Single A
One row with a single A — the prefix loop never enters.
scanf
Leading space in format
Use scanf(" %c", &end) so a leftover newline is not read as the letter.
Off-by-one
n vs letter count
n = end - 'A' is the last index. Letter count and row count are both n + 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline parts (Examples 1–2)
O(n²)
O(1)
Helper (Example 3)
O(n²)
O(1)
For n + 1 letters there are n + 1 rows and each row prints n + 1 characters — quadratic in the letter count.
Remember
Key Takeaways
Two parts: descending i..1, then ascending 0..n-i.
Skip A in the prefix:j > 0 prevents a duplicated center join.
Constant width:i + (n - i + 1) = n + 1 on every row.
Complexity:O(n²) time; O(1) extra space.
One line: for each i from 0 to n, print alpha[i]..alpha[1], then alpha[0]..alpha[n-i], then printf("\n").
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). Together with the prefix of length i, every row has 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.
printf("%c", ch) prints a letter and stays on the same line. printf("\n") ends the current line after both parts finish.
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.
Check scanf(" %c", &end) == 1, require a single A–Z character, and reject invalid input.
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 index i (0..n), print a descending prefix from alpha[i] down to alpha[1] (skip A), then an ascending suffix from A up to alpha[n - i]. Prefix length i plus suffix length n - i + 1 always equals row width n + 1.