Palindromic Number Pyramid in C#

What You’ll Learn
How to print a centered palindromic number pyramid in C# where each row goes up from 1 to i and then back down to 1.
You’ll learn how spacing, ascending loops, and descending loops combine to form a symmetric pyramid.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1\n 1 2 1\n 1 2 3 2 1\n1 2 3 4 3 2 1\n1 2 3 4 5 4 3 2 1Complete C# Program
Print leading spaces for centering, then print 1..i and i-1..1 to form the palindrome on each row.
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();
}
}
}
}🧠 How It Works
Pick the number of rows
rows controls the pyramid height.
Add leading spaces
For row i, print rows-i space groups to center the numbers.
Print ascending numbers
Print 1..i using a loop.
Print descending numbers
Then print i-1..1 to make the row palindromic.
Palindromic pyramid
Total printed values are about O(rows²), so runtime grows quadratically with the row count.
Variation — User Input Rows
Let the user decide the pyramid height at runtime with input validation.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter 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();
}
}
}
}💡 Tips for Enhancement
Try These
- Use fixed-width formatting (e.g.,
Console.Write($"{k,2} ")) for 2-digit rows - Remove trailing spaces by building a string and trimming it before printing
- Print a full diamond by adding a mirrored bottom half
- Replace numbers with characters to build alphabet palindromes
Avoid
- Skipping input validation (zero/negative rows don’t make sense)
- Using single spaces for centering when numbers include spaces after them (alignment breaks)
- Printing the peak twice when extending to a full diamond
Key Takeaways
Centering is done by printing rows-i groups of spaces before each row.
Each row is built from two sequences: 1..i and i-1..1.
The pyramid is palindromic: it reads the same left-to-right and right-to-left.
Runtime grows like O(rows²) due to nested loops.
❓ Frequently Asked Questions
k + " "). Using two spaces per gap keeps the pyramid visually centered.TrimEnd() it before printing, or conditionally print spaces between numbers only.rows-1 down to 1 to mirror the pyramid.Explore More C# Number Patterns!
Try extending this pyramid into a full diamond or printing it with different spacing styles.
Palindromic patterns appear in many problems: if you can print 1..i..1, you can print palindromic alphabets, stars, and even mirrored sequences with the same idea.
12 people found this page helpful
