A mirrored alphabet with spaces prints a growing left ramp and a matching right mirror, separated by a shrinking band of spaces until the last row meets as ABCDEEDCBA.
Remember
Rule: left A..i + spaces + right i..A (gap shrinks)
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBA ← top = E
Unlike Program 18 (a continuous palindrome with no gap), this pattern keeps a middle space band that shrinks by two columns each row until both halves touch.
Approach
How to Solve It
For each peak index, print a left half, a space gap, and a mirrored right half — all aligned to a fixed total width.
Method
Idea
Best for
Dual fixed scan
Left: letter if j <= i else space; right: space if k > i else letter
Learning column conditions
Explicit gap
Print left letters, 2*(n-i) spaces, then reverse letters
Clearer reading once the shape is clear
Pseudocode
Pseudocode
n = top - 'A'
for i from 0 to n:
for j from 0 to n:
print letter j if j <= i else space
for k from n down to 0:
print space if k > i else letter k
print newline
Cheat sheet
Goal
Pattern
Half-width index
int n = top - 'A';
Left ramp
Console.Write(j <= i ? alpha[j] : ' ');
Right ramp
Console.Write(k > i ? ' ' : alpha[k]);
Explicit gap
for (int s = 0; s < 2 * (n - i); s++) Console.Write(' ');
End the row
Console.WriteLine();
Last-row meet
When i == n, gap is 0 → ABCDEEDCBA
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each letter and each space cell
Console.WriteLine
Ends the current line
After left + right (or left + gap + right)
Try it
Live Preview
Change the top letter and the mirrored space pattern updates instantly — including half-width and gap size.
Enter one letter A–J. Half-width is n = top - 'A'; each row is width 2 × (n + 1).
Live resulttop E · width 10 · 5 rows
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBA
Trace
Worked Walkthrough — top = 'E' (n = 4)
Trace each peak index, the left half, the gap, and the mirrored right half.
i
Left
Gap
Right
Printed row
0
A
8
A
A A
1
AB
6
BA
AB BA
2
ABC
4
CBA
ABC CBA
3
ABCD
2
DCBA
ABCD DCBA
4
ABCDE
0
EDCBA
ABCDEEDCBA
Gap size is 2*(n - i). On the last row the halves meet and the peak letter appears twice (EE).
Code
C# Programs
Three complete programs: fixed A–E dual scan, top-letter input, and an explicit gap form. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Two fixed-width scans per row. Conditions decide whether to print a letter or a space.
C#
using System;
class Program
{
static void Main()
{
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 4; j++)
{
if (j <= i)
Console.Write(alpha[j]);
else
Console.Write(" ");
}
for (int k = 4; k >= 0; k--)
{
if (k > i)
Console.Write(" ");
else
Console.Write(alpha[k]);
}
Console.WriteLine();
}
}
}
Output
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBA
How It Works
1. Outer loop grows the peak.i runs 0..4 so peaks are A through E.
2. Left scan. Print alpha[j] while j <= i; otherwise print a space to fill the half-width.
3. Right scan. Print spaces while k > i, then print descending letters. When i = 4, every column is a letter → ABCDEEDCBA.
Example 2 — Top Letter Input
Build the full width dynamically from the chosen top letter. Prefer validating a single A–Z character in real apps.
C#
using System;
class Program
{
static void Main()
{
Console.Write("Enter the top letter (like E): ");
char top = Convert.ToChar(Console.ReadLine());
int n = top - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
Console.Write(j <= i ? alpha[j] : ' ');
for (int k = n; k >= 0; k--)
Console.Write(k > i ? ' ' : alpha[k]);
Console.WriteLine();
}
}
}
Output (when user enters C)
Enter the top letter (like E): C
A A
AB BA
ABCCBA
How It Works
1. Map top → half-width.n = top - 'A' sets the shared scan length for both halves.
2. Same dual-scan core. Ternary form of Example 1 — only the bounds follow n.
3. Safer input tip. Prefer a single uppercase letter:
Safer input
string raw = Console.ReadLine()?.Trim() ?? "";
if (raw.Length != 1 || raw[0] < 'A' || raw[0] > 'Z')
{
Console.WriteLine("Enter one letter A–Z.");
return;
}
char top = raw[0];
Example 3 — Letters, Gap Count, Mirror
Often clearer to read: print left letters, print 2*(n-i) spaces, then print the reverse letters.
C#
using System;
class Program
{
static void Main()
{
int n = 4; // last index (E)
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= i; j++)
Console.Write((char)('A' + j));
for (int s = 0; s < 2 * (n - i); s++)
Console.Write(' ');
for (int k = i; k >= 0; k--)
Console.Write((char)('A' + k));
Console.WriteLine();
}
}
}
Output
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBA
How It Works
1. Left letters only. Print A..i without padding inside the half.
2. Explicit gap.2*(n - i) spaces replace the leftover columns from both fixed scans.
3. Mirror from the peak. Print i..A. On the last row the gap is 0, so the peak letter appears twice.
Edge Cases & Pitfalls
Check these before calling the solution done.
Gap = n - i
Half-width gap
Forgetting the 2 * in the explicit form leaves only half the middle spaces — the mirror shifts left.
Right from i-1
Missing peak twin
Starting the right half at i - 1 removes the doubled center on the last row. Only do that if the problem asks for it.
WriteLine early
Broken row
Call WriteLine only after both halves. Inside any inner loop, each cell lands on its own line.
top = 'A'
Single AA
Output is AA (no gap) — a good sanity check.
No spaces
Wrong pattern
Skipping the space branches collapses into a continuous palindrome each row — that is Program 18, not this one.
Empty input
Convert.ToChar crash
Validate length before converting. Prefer a single A–Z character.
Analysis
Time and Space Complexity
Program
Time
Extra space
Dual scan / explicit gap
O(n²)
O(1)
For half-width n + 1 letters, each of n + 1 rows prints 2(n + 1) characters — quadratic in the letter count.
Remember
Key Takeaways
Three parts: left ramp, shrinking space gap, right mirror.
Gap formula:2*(n - i) spaces between the halves.
Final meet: last row has gap 0 and a doubled peak letter.
Complexity:O(n²) time; O(1) extra space.
One line: for each peak, print left letters, a shrinking even gap, then the mirrored right letters.
Frequently Asked Questions
The left loop builds the increasing part and fills the remaining columns with spaces. The right loop fills spaces until the peak, then prints the decreasing mirror.
The spaces keep both halves fixed width so the mirror effect is aligned. The gap shrinks each row until both halves touch.
When i reaches the last letter (E), all positions satisfy the letter conditions on both sides, so both halves print letters and meet as ABCDEEDCBA.
Increase the last index/letter and update the loop bounds so the left and right halves each scan the new width.
Console.Write stays on the same line for each cell. Console.WriteLine ends the row after both halves finish.
Program 18 prints a continuous palindrome with no middle gap. This pattern keeps a shrinking space band between left and right ramps until the final row.
O(n²) for n letters because each row scans n columns twice (left + right).
Read a string, take the first character, require A–Z, and reject empty input. Cap at Z if you only want alphabetic ranges.
🤔
Did you know?
Each row uses two fixed-width scans from A to E. The first builds the left ramp (letters when j <= i else spaces). The second builds the right ramp (spaces while k > i, else letters). The gap shrinks until the last row meets as ABCDEEDCBA.