C# Palindromic Number Pyramid Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

Remember
Rule: spaces (rows−i)×2, then 1..i, then i−1..1

        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     ← rows = 5

In C# print (rows - i) pairs of spaces, then Console.Write(k + " ") ascending and descending, then WriteLine().

How to Solve It

Per row: center with leading spaces, climb to the peak, then mirror down without repeating the peak.

MethodIdeaBest for
Three inner loopsSpaces, ascending 1..i, descending i-1..1Learning, interviews, exams
Rows inputSame logic with a user-chosen heightPractice / 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

GoalPattern
Outer loopfor (i = 1; i <= rows; i++)
Leading spacesfor (s = 1; s <= rows - i; s++) Console.Write(" ");
Ascending halffor (k = 1; k <= i; k++) Console.Write(k + " ");
Descending halffor (k = i - 1; k >= 1; k--) Console.Write(k + " ");
Digits on row i2 * i - 1

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineSpaces and each digit plus trailing space
Console.WriteLineEnds the current lineAfter spaces + ascending + descending

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 result rows = 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 

Worked Walkthrough — Row i = 3 when rows = 5

Trace one middle row so centering and the skip-peak descent are clear.

StepLoopPrints
1s = 1..2 (rows - i) (4 spaces)
2k = 1..31 2 3
3k = 2..12 1

Full row: 1 2 3 2 1. Peak 3 appears once because descent starts at i - 1.

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();
            }
        }
    }
}

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();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 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();
            }
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Diagonal Mirror Number Pyramid

Continue with a pyramid that prints the row number only on the left and right diagonals.

Program 57 tutorial →

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.

6 people found this page helpful