Shape Rule
Palindromic row
Row i prints 1..i ascending, then i-1..1 descending — always reads the same forward and backward.

Program 56 prints a centered palindromic number pyramid: each row shows 1..i..1 with leading spaces for centering — a natural step after Program 55’s column-wise triangle. This tutorial covers spacing, ascending and descending loops, a live preview, worked C# examples, edge cases, and complexity.
Palindromic row
Row i prints 1..i ascending, then i-1..1 descending — always reads the same forward and backward.
i = 1..rows
for (i = 1; i <= rows; i++) picks the current row and its palindromic width.
s = 1..(rows-i)
Console.Write(" ") repeated rows - i times — centers the pyramid.
k = 1..i
for (k = 1; k <= i; k++) Console.Write(k + " ") — counts up to the peak.
k = i-1..1
for (k = i - 1; k >= 1; k--) Console.Write(k + " ") — mirrors without repeating the peak.
Complexity
Row i prints about 2i-1 digits plus spaces — total work grows as O(n²).
A palindromic number pyramid prints a centered triangle where row i shows digits from 1 up to i and back down to 1. With rows = 5, you get 1, 1 2 1, 1 2 3 2 1, and so on — each row wider and centered with leading spaces.
In C# use an outer loop for rows, print (rows - i) space pairs, then ascending 1..i, then descending i-1..1, before WriteLine().
It bridges Program 55’s column-wise fill to centered symmetry — combining spacing with ascending and descending loops on each row.
(rows - i) pairs of spaces before digits.
k = 1..i prints up to the peak.
Program 55 uses column-wise 2D array fill; Program 56 prints palindromic rows with spacing.
Follow Program 55; continue to Program 57 next.
In short: outer i = 1..rows, spaces rows-i, ascending 1..i, descending i-1..1, then WriteLine().
Given row count rows = 5, print a centered palindromic number pyramid — row i shows 1..i..1 with leading spaces.
// rows = 5
// 1
// 1 2 1
// 1 2 3 2 1
//1 2 3 4 3 2 1
//1 2 3 4 5 4 3 2 1 | Item | Type | Description |
|---|---|---|
rows | int | Pyramid height — bottom row has rows as peak digit. |
i (outer) | int | Current row index — runs 1 to rows. |
s (spaces) | int | Prints (rows - i) pairs of spaces for centering. |
k (ascending) | int | Prints 1..i with trailing space after each digit. |
k (descending) | int | Prints i-1..1 — skips repeating the peak digit. |
| Row width | int | Row i has 2i-1 digits plus leading spaces. |
for i from 1 to rows:
print (rows - i) pairs of spaces
for k from 1 to i:
print k
for k from i - 1 down to 1:
print k
print newline | Approach | Idea | Best for |
|---|---|---|
| Three inner loops | Spaces, ascending 1..i, descending i-1..1 | Learning and interviews |
| Skip peak in descent | k = i-1..1 avoids duplicate peak digit | Clean palindromic rows |
| User-input rows | int.TryParse(...) | Flexible pyramid size |
| Compact trace | rows = 3 on paper first | Quick dry-runs before full demo |
| Full diamond | Mirror bottom half from rows-1..1 | Extension after mastering pyramid |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Leading spaces | for (s = 1; s <= rows - i; s++) Console.Write(" "); |
| Ascending half | for (k = 1; k <= i; k++) Console.Write(k + " "); |
| Descending half | for (k = i - 1; k >= 1; k--) Console.Write(k + " "); |
| End row | Console.WriteLine(); |
| Program 55 contrast | Program 55 uses column-wise 2D fill; Program 56 uses centered palindromic rows |
Same centered pyramid — three ways to set row count and trace the logic.
rows = 5Hard-coded height for demos
TryParseRead row count from console
rows = 3Quick dry-run on paper
rows - iSpace pairs before digits
1..i..1Digits per row i
Reach for this pattern when teaching centered output, palindromic sequences, and combining spacing with multiple inner loops.
Natural follow-up after Program 55’s column-wise triangle — introduces centering and palindromic rows.
Each row reads symmetrically — good bridge to string palindrome problems.
Spaces plus ascending and descending halves — concrete nested-loop practice.
Compare this centered pyramid with the next pattern in the series.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in centering, palindromic rows, and O(n²) thinking.
Choose row count between 3 and 9 and draw the centered palindromic number pyramid in the browser.
Three complete C# programs — fixed rows, user input, and a compact trace demo. Click View Output to reveal sample console results.
Print a centered palindromic pyramid with five rows — spaces, ascending, and descending loops per row.
rows = 5Hard-coded row count — print leading spaces, then ascending 1..i, then descending i-1..1.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = 1; i <= rows; i++)
{
for (int s = 1; s <= (rows - i); s++)
Console.Write(" ");
for (int k = 1; k <= i; k++)
Console.Write(k + " ");
for (int k = i - 1; k >= 1; k--)
Console.Write(k + " ");
Console.WriteLine();
}
}
}
} When i = 3, print 2 space pairs, then 1 2 3, then 2 1 — output 1 2 3 2 1. When i = 1, only the ascending loop runs and the descending loop is skipped.
Read row count from the console with safe parsing.
Read rows from the console with int.TryParse — reject invalid input gracefully.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (int i = 1; i <= rows; i++)
{
for (int s = 1; s <= (rows - i); s++)
Console.Write(" ");
for (int k = 1; k <= i; k++)
Console.Write(k + " ");
for (int k = i - 1; k >= 1; k--)
Console.Write(k + " ");
Console.WriteLine();
}
}
}
} Same spacing and palindromic loop core as Example 1; only the source of rows changes from a literal to user input.
Smaller row count for quick tracing on paper or in interviews.
rows = 3Use rows = 3 to trace spacing, ascending, and descending loops before scaling to 5 rows.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
for (int i = 1; i <= rows; i++)
{
for (int s = 1; s <= (rows - i); s++)
Console.Write(" ");
for (int k = 1; k <= i; k++)
Console.Write(k + " ");
for (int k = i - 1; k >= 1; k--)
Console.Write(k + " ");
Console.WriteLine();
}
}
}
} With only three rows you can trace every space pair and digit loop on paper before running the full rows = 5 demo.
int rows = 5; controls pyramid height and spacing width.
for (s = 1; s <= rows - i; s++) Console.Write(" "); — centers row i.
for (k = 1; k <= i; k++) Console.Write(k + " "); — counts up to the peak.
for (k = i - 1; k >= 1; k--) Console.Write(k + " "); then WriteLine().
Row i prints 2i-1 digits — O(n²) time, O(1) extra memory.
rows = 5Trace each row’s space count, ascending half, descending half, and full line output.
i | Space pairs | Ascending | Descending | Row output |
|---|---|---|---|---|
1 | 4 | 1 | (skip) | 1 |
2 | 3 | 1 2 | 1 | 1 2 1 |
3 | 2 | 1 2 3 | 2 1 | 1 2 3 2 1 |
4 | 1 | 1 2 3 4 | 3 2 1 | 1 2 3 4 3 2 1 |
5 | 0 | 1 2 3 4 5 | 4 3 2 1 | 1 2 3 4 5 4 3 2 1 |
Row i always prints exactly 2i-1 digits — a palindromic line built from spacing plus two inner loops.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Spacing plus ascending and descending loops — three inner loops per row.
Example: trace each row in the walkthrough table — spaces, ascending, descending.
Each row reads symmetrically — compare with Program 52's left-aligned palindrome.
Example: row 5 has no leading spaces and shows 1 2 3 4 5 4 3 2 1.
Practice Write vs WriteLine with multiple values per row.
Example: put WriteLine inside the inner loop by mistake.
Space pairs on row i = rows - i — fewer spaces as the pyramid widens.
Example: Peak row 10 has 9 space pairs on row 1 — 19 digits on the bottom row.
Growing inner bound makes O(n²) concrete — count prints for n rows.
Example: Peak row 5 bottom line has 9 digits — see the walkthrough table.
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Each row is instantly recognizable as a palindrome — spacing, ascending, and descending halves make the shape obvious.
Centering with spaces teaches real console alignment — not abstract loop drill.
Mirror the bottom half from rows-1..1 to build a full diamond, or use fixed-width formatting for larger peaks.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace rows = 3 on paper — row 2 shows 1 2 1 with 1 space pair.
Small habits that keep number-pattern code clean.
Print (rows - i) pairs of two spaces before digits on row i.
Avoid crashes when the user types letters instead of a number.
Only call WriteLine() after both inner loops finish the row.
Use for (k = i - 1; k >= 1; k--) so the peak digit is not printed twice.
Trace five rows on paper before coding the full 10-row demo.
Pro Tip: if the output is a vertical list of single numbers, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break palindromic number pyramid patterns.
Each digit lands on its own line — you get a column, not a pyramid.
→ Use Console.Write(k + " ") for digits; WriteLine only after all three inner loops.
Starting descending at k = i prints the peak digit twice — row looks like 1 2 2 1.
→ Use for (k = i - 1; k >= 1; k--) — skip the peak in the descending half.
Without leading spaces the pyramid is left-aligned, not centered.
→ Print (rows - i) pairs of two spaces before the digits on row i.
All numbers print on one long line without row breaks.
→ Add Console.WriteLine() after both inner loops complete.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — the descending loop does not run.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Bottom row has 9 digits — good for dry-runs before scaling up.
Convert.ToInt32 throws — use TryParse.
Row 9 scans 17 character positions — total work grows as O(n²).
Try these variations to lock in the pattern.
Console.Write($"{k,2} ") for rows beyond 9s = 1..(rows-i). Ascending: k = 1..i. Descending: k = i-1..1.Console.Write stays on the line; WriteLine advances — call it only after all three inner loops finish.rows > 0 for interactive programs; rows = 1 prints a single centered 1.i prints 2i-1 digits — total work grows as O(n²) for n rows.Quick Takeaway: spaces rows-i, ascending 1..i, descending i-1..1, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Digits on row i | 2i - 1 | No storage beyond loop counters |
The palindromic number pyramid is a natural follow-up to Program 55: centered rows built with spacing, ascending, and descending loops. Master the fixed-rows version, then try user input and the compact 3-row trace.
Practice the three examples above, then continue to Program 57 for the next pattern in the series.
Row i prints 2i-1 palindromic digits — centered with (rows-i) space pairs.
for (s = 1; s <= rows - i; s++) Console.Write(" ");for (k = 1; k <= i; k++) Console.Write(k + " ");for (k = i - 1; k >= 1; k--) Console.Write(k + " ");WriteLine() after all three inner loopsint.TryParse for user inputk = i — duplicates the peak digitWriteLine inside any inner looprows = 3 dry-run before coding rows = 5Print the centered pyramid the beginner-friendly way.
1..i..1 per row
Definitionrows - i spaces
Codek = 1..i
Codek = i-1..1
LogicO(n²) time
AnalysisEach row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total prints grow as O(n²) for n rows.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful