Palindromic Number Pyramid in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spacing + Symmetry

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:

Output
      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 1
1

Complete C# Program

Print leading spaces for centering, then print 1..i and i-1..1 to form the palindrome on each row.

C#
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

1

Pick the number of rows

rows controls the pyramid height.

Setup
2

Add leading spaces

For row i, print rows-i space groups to center the numbers.

Centering
3

Print ascending numbers

Print 1..i using a loop.

Up
4

Print descending numbers

Then print i-1..1 to make the row palindromic.

Down
=

Palindromic pyramid

Total printed values are about O(rows²), so runtime grows quadratically with the row count.

2

Variation — User Input Rows

Let the user decide the pyramid height at runtime with input validation.

C#
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

1

Centering is done by printing rows-i groups of spaces before each row.

2

Each row is built from two sequences: 1..i and i-1..1.

3

The pyramid is palindromic: it reads the same left-to-right and right-to-left.

4

Runtime grows like O(rows²) due to nested loops.

❓ Frequently Asked Questions

Because each number is printed with a trailing space (k + " "). Using two spaces per gap keeps the pyramid visually centered.
Build a string for the line and TrimEnd() it before printing, or conditionally print spaces between numbers only.
Yes. Add another outer loop that prints rows from rows-1 down to 1 to mirror the pyramid.
O(rows²), because each row prints O(rows) values/spaces and there are rows rows.

Explore More C# Number Patterns!

Try extending this pyramid into a full diamond or printing it with different spacing styles.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful