Palindrome Number Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a palindrome number triangle where each row mirrors itself: 1, 212, 32123, 4321234, and so on.

This is a clean exercise for understanding how to combine two loops to build a symmetric sequence on each row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
212
32123
4321234
543212345
1

Complete C# Program

First print descending numbers from i to 2, then print ascending numbers from 1 to 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

Choose the number of rows

rows controls how many lines are printed.

Setup
2

Outer loop (row number)

for (i = 1; i <= rows; i++) prints one palindrome row per iteration.

Row control
3

Left half (descending)

for (j = i; j > 1; j--) prints the left side: i, i-1, ..., 2.

Mirror left
4

Right half (ascending)

for (j = 1; j <= i; j++) completes the palindrome: 1, 2, ..., i.

Mirror right
=

Palindrome rows

Each row has \(2i-1\) digits, so total printed digits grow like O(n²).

2

Variation — User Input Rows

Let the user choose the number of rows at runtime.

C#
using System;

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

            Console.Write("Enter number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                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();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits (e.g., print j + " ") for readability
  • Center-align the triangle by printing leading spaces before each row
  • Print odd-only palindromes by changing the loop increments
  • Replace numbers with characters to create palindrome alphabet patterns

Avoid

  • Printing a newline inside the inner loops
  • Using invalid row counts without input validation
  • Mixing loop directions (keep one descending and one ascending)

Key Takeaways

1

Two loops per row build the palindrome: descending then ascending.

2

The center digit is always 1, so each row is symmetric.

3

Row \(i\) prints \(2i-1\) digits.

4

Total work grows as O(n²) for n rows.

❓ Frequently Asked Questions

The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Yes. Print Console.Write(j + " ") in both inner loops. You can trim trailing spaces if needed.
Print leading spaces before the descending loop. For example, print rows - i spaces (or a larger width if you add digit spacing).
O(n²) for n rows because total printed digits are proportional to \(1 + 3 + 5 + \dots + (2n-1) = n^2\).

Explore More C# Number Patterns!

Learn more palindrome and symmetry-based patterns to strengthen your loop logic.

All Number Patterns →
Did you know?

The sum of the first \(n\) odd numbers is \(n^2\). This pattern prints \(1, 3, 5, \dots, 2n-1\) digits per row, so total digits printed is \(n^2\).

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