A palindromic number pyramid is a centered triangle where row i prints 1..i ascending, then i-1..1 descending — so every row reads the same forward and backward.
In C# print (rows - i) pairs of spaces, then Console.Write(k + " ") ascending and descending, then WriteLine().
Approach
How to Solve It
Per row: center with leading spaces, climb to the peak, then mirror down without repeating the peak.
Method
Idea
Best for
Three inner loops
Spaces, ascending 1..i, descending i-1..1
Learning, interviews, exams
Rows input
Same logic with a user-chosen height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
print (rows - i) pairs of spaces
for k from 1 to i:
print k and a space
for k from i - 1 down to 1:
print k and a space
print newline
Cheat sheet
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 + " ");
Digits on row i
2 * i - 1
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Spaces and each digit plus trailing space
Console.WriteLine
Ends the current line
After spaces + ascending + descending
Try it
Live Preview
Change the row count and the centered palindromic pyramid updates instantly — capped at 9 for readable single-digit demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · bottom peak 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
Trace
Worked Walkthrough — Row i = 3 when rows = 5
Trace one middle row so centering and the skip-peak descent are clear.
Step
Loop
Prints
1
s = 1..2 (rows - i)
(4 spaces)
2
k = 1..3
1 2 3
3
k = 2..1
2 1
Full row: 1 2 3 2 1. Peak 3 appears once because descent starts at i - 1.
Code
C# Programs
Three complete programs: fixed rows = 5, user-input rows, and a compact rows = 3 demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Hard-coded height — spaces, ascending, then descending on each row.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, s, k;
for (i = 1; i <= rows; i++)
{
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 + " ");
Console.WriteLine();
}
}
}
}
Output
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
How It Works
1. Center. Print (rows - i) pairs of spaces so upper rows sit further right.
2. Ascend.Console.Write(k + " ") from 1 to i reaches the peak.
3. Descend. Start at i - 1 so the peak is not printed twice, then WriteLine().
Example 2 — User Input (rows)
Read the row count and draw the same centered palindromic pyramid.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, i, s, k;
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= rows; i++)
{
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 + " ");
Console.WriteLine();
}
}
}
}
Output (when user enters 4)
Enter the number of rows: 4
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
2. Same core. Spacing and palindrome loops match Example 1 — only rows comes from the user.
3. Single-digit tip. Cap demos at rows ≤ 9 so every digit stays one character wide.
Example 3 — Compact rows = 3
Same structure with only three rows — easy to trace on paper.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, s, k;
for (i = 1; i <= rows; i++)
{
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 + " ");
Console.WriteLine();
}
}
}
}
Output
1
1 2 1
1 2 3 2 1
How It Works
1. Three rows. Row 1: four leading spaces + 1. Row 2: two spaces + 1 2 1.
2. Trace on paper. Confirm descent starts at i - 1 so row 3 is 1 2 3 2 1, not 1 2 3 3 2 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = i..1
Double peak
If descent starts at i, row 3 becomes 1 2 3 3 2 1. Always start at i - 1.
one space
Weak centering
Use Console.Write(" ") (two spaces) per indent so digits align under each other with k + " ".
WriteLine
Broken rows
If WriteLine sits inside an inner loop, each digit lands on its own line. Call it only after both digit loops finish.
rows = 1
Single digit
Output is just 1 with no leading spaces — a good sanity check for input validation.
rows > 9
Multi-digit width
Digits 10+ break neat alignment with k + " ". Cap demos at 9 or use a fixed field width.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Row i prints O(i) spaces and digits; summing over n rows is O(n²). Only a few loop variables are needed.
Remember
Key Takeaways
Three parts per row: leading spaces, ascending 1..i, descending i-1..1.
Skip the peak: descent starts at i - 1 so the middle digit appears once.
Write vs WriteLine: spaces and digits stay on the line; WriteLine advances after the full row.
Next step: Program 57 prints the row number only on left and right diagonals.
One line: spaces (rows-i), then 1..i, then i-1..1, then WriteLine().
Frequently Asked Questions
Each row reads the same forward and backward: 1, 1 2 1, 1 2 3 2 1, and so on.
Print (rows - i) pairs of spaces before the numbers on row i — more spaces on upper rows, fewer on lower rows.
A centered pyramid where row i shows 1..i ascending then i-1..1 descending, e.g. row 3: ' 1 2 3 2 1 '.
Program 55 fills a 2D array column-wise. Program 56 prints palindromic digits directly with spaces, ascending, and descending loops.
Starting at i-1 avoids printing the peak digit twice — row i already printed i in the ascending loop.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because each row prints O(n) spaces and digits.
Yes. Print this pyramid for the top half, then mirror rows from rows-1 down to 1 for the bottom half.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
One centered row prints a single 1 with (rows-1)*2 = 0 leading spaces.
🤔
Did you know?
Each row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total work grows as O(n²) for n rows.