C# Palindrome Number Triangle Pattern

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

What Is This Pattern?

A palindrome number triangle prints digits that rise to a peak and then fall, so each row reads the same forwards and backwards.

Remember
Rule: print 1..i then i-1..1

1
121
12321
1234321
123454321   ← 5 rows

In C# you use two inner loops: one ascending (j = 1..i) and one descending (k = i-1..1), then Console.WriteLine() ends the row. Starting the mirror at i - 1 keeps the peak digit from printing twice.

How to Solve It

One two-loop idea with two useful variants — packed digits and spaced digits.

MethodIdeaBest for
Ascend + mirrorPrint 1..i, then i-1..1 with WriteLearning, interviews, exams
Spaced digitsSame loops; print j + " " and k + " "Practice variants

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j
    for k from i - 1 down to 1:
        print k
    print newline

Cheat sheet

GoalPattern
Grow the rowfor (i = 1; i <= rows; i++)
Left halffor (j = 1; j <= i; j++) Console.Write(j);
Mirror halffor (k = i - 1; k >= 1; k--) Console.Write(k);
End the rowConsole.WriteLine();
Add spacesConsole.Write(j + " "); in both loops

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit j or k
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the palindrome triangle updates instantly — including the total digit count.

Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 25 digits
1
121
12321
1234321
123454321

Worked Walkthrough — rows = 4

Trace each outer-loop value of i: the ascending half, the mirror half, and the full row.

iAscend 1..iMirror i-1..1Printed row
11—1
2121121
31232112321
412343211234321

Total digits are 1+3+5+7 = 16 — equal to rows² for any rows.

C# Programs

Three complete programs: fixed height, user-input height, and spaced digits. Use View Output for sample results.

Example 1 — Fixed rows = 5

Ascend to the peak, then mirror down without repeating the middle digit.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k;

            for (i = 1; i <= 5; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }

                for (k = i - 1; k >= 1; k--)
                {
                    Console.Write(k);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Ascend. When i = 3, the first loop prints 123.

2. Mirror. The second loop starts at i - 1 and prints 21, forming 12321.

3. Newline. WriteLine() after both loops starts the next longer palindrome.

Example 2 — User Input

Read the row count at runtime. Prefer int.TryParse so bad input does not throw.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j, k;

            Console.Write("Enter 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 (j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }

                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 two loops. Four rows end at 1234321.

Example 3 — Spaced Digits

Same palindrome shape — only the print statements add spaces.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j, k;

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= i; j++)
                {
                    Console.Write(j + " ");
                }

                for (k = i - 1; k >= 1; k--)
                {
                    Console.Write(k + " ");
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same structure. Ascend then mirror — only the print format changes.

2. Add gaps. Write(j + " ") and Write(k + " ") insert spaces between digits.

Edge Cases & Pitfalls

Check these before calling the solution done.

double peak

k = i instead of k = i - 1

That repeats the middle digit (123321 instead of 12321). Keep k = i - 1.

missing mirror

Only the ascending loop

Without the descending loop you get a plain triangle (1, 12, 123…), not a palindrome.

WriteLine early

WriteLine between the two loops

That splits the palindrome across two lines. Call WriteLine() only after both loops finish.

rows = 1

Single 1

Output is just 1 — the mirror loop never runs.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Packed digits (Examples 1–2)O(n²)O(1)
Spaced digits (Example 3)O(n²)O(1)

Row i prints 2i - 1 digits; the sum 1+3+5+…+(2n-1) equals n².

Key Takeaways

  • Rule: print 1..i, then i-1..1, for each row.
  • Start at i - 1: that is what keeps the peak digit from doubling.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after both loops.
  • Next step: Program 28 builds a 0-centered descending mirror.

One line: for each row, print 1..i then i-1..1 with Write, then WriteLine.

Frequently Asked Questions

Starting from i-1 avoids repeating the middle number. For example, for i=3, printing 123 then 21 makes 12321.
Use Console.Write(j + " ") and Console.Write(k + " ") in the loops instead of Console.Write(j) — see Example 3.
The ascending loop prints 1..i and the descending loop prints i-1..1 — together they read the same forwards and backwards.
Console.Write prints digits on the same line. Console.WriteLine ends the row after both inner loops finish.
One loop handles the ascending half (j) and the other handles the mirror half (k) — clearer than a single complex loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because row i prints 2i-1 digits and the total is 1+3+5+…+(2n-1) = n².
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1 with no mirror half.

Did you know?

This palindrome triangle prints 1..i and then i-1..1 on each row. The second loop mirrors the first, producing outputs like 12321 and 123454321.

Next: 0-Centered Descending Mirror

Mirror descending digits around a center zero on each row.

Program 28 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.

12 people found this page helpful