C# Palindrome Number Triangle Pattern (Outer Peak)

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

What Is This Pattern?

A palindrome number triangle prints digits that read the same forward and backward on each row. Build the left half by counting down, then the right half by counting up through 1.

Remember
Rule: print i..2, then 1..i  (center is always 1)

1
212
32123
4321234
543212345   ← 5 rows

In C# use two inner loops per row: j from i down to 2, then j from 1 up to i. Row i prints 2i - 1 digits with no spaces between them.

How to Solve It

One outer loop and two inner loops that mirror around 1.

MethodIdeaBest for
Two-loop palindromeDescend to 2, then ascend from 1Learning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

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

Cheat sheet

GoalPattern
Grow the rowfor (i = 1; i <= rows; i++)
Left halffor (j = i; j > 1; j--) Console.Write(j);
Right halffor (j = 1; j <= i; j++) Console.Write(j);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit (no spaces)
Console.WriteLineEnds the current lineAfter both inner loops

Live Preview

Change the row count and the palindrome triangle updates instantly — including 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
212
32123
4321234
543212345

Worked Walkthrough — rows = 4

Trace the left descent and the right ascent. The center digit is always 1.

iLeft / rightPrinted row
1(none) / 11
22 / 12212
332 / 12332123
4432 / 12344321234

The left loop stops at 2 so the ascending loop can print the center 1 once — never twice.

C# Programs

Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Two inner loops per row: descend to 2, then ascend from 1 through i.

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j > 1; j--)
                    Console.Write(j);

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i grows from 1 to 5 — each row is a longer palindrome.

2. Left half. j runs from i down to 2 (skipped entirely when i = 1).

3. Right half. j runs from 1 to i, placing the center 1 and the ascending mirror.

Example 2 — User Input

Read the row count and apply the same two-loop palindrome for any height.

C#
using System;

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

            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 = i; j > 1; j--)
                    Console.Write(j);

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same palindrome. Three rows end at 32123.

Example 3 — Compact rows = 3

A smaller fixed demo — same two-loop idea, easier to trace by hand.

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j > 1; j--)
                    Console.Write(j);

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Left prints i..2; right prints 1..i.

2. Quick check. The longest row is 32123 — five digits for i = 3.

Edge Cases & Pitfalls

Check these before calling the solution done.

double 1

Descend all the way to 1

Using j >= 1 on the left prints two 1s. Stop at j > 1 so the ascending loop owns the center.

WriteLine early

WriteLine between the two halves

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

extra spaces

Print a space after each digit

This pattern is tight digits only. Use Console.Write(j) with no trailing space.

rows = 1

Single 1

The left loop does not run; only the ascending loop prints 1.

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
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Total digits are 1 + 3 + 5 + … + (2n-1) = n², so work is quadratic in n.

Key Takeaways

  • Rule: print i..2, then 1..i — the center is always 1.
  • Length: row i has 2i - 1 digits; all rows together print n² digits.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
  • Next step: Program 38 prints a decreasing-length triangle of continuous numbers.

One line: for each row i, print a descending left half and an ascending right half that meet at 1.

Frequently Asked Questions

Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop prints i down to 2 (left half). Then another loop prints 1 up to i (right half). Together they create a mirrored sequence.
The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Row i prints 2i - 1 digits — one more than the previous row's digit count by 2.
Program 36 is right-aligned with decreasing sequences. Program 37 builds a symmetric palindrome on each row with two inner loops.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total digits printed are 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 digit 1.

Did you know?

Each row is a palindrome: print i down to 2, then 1 up to i. Row i prints 2i - 1 digits — total digits across all rows = n².

Next: Decreasing Continuous Triangle

Print continuous numbers in rows that get shorter each time.

Program 38 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