Palindrome Number Triangle in C#

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:
1
212
32123
4321234
543212345Complete C# Program
First print descending numbers from i to 2, then print ascending numbers from 1 to i.
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
Choose the number of rows
rows controls how many lines are printed.
Outer loop (row number)
for (i = 1; i <= rows; i++) prints one palindrome row per iteration.
Left half (descending)
for (j = i; j > 1; j--) prints the left side: i, i-1, ..., 2.
Right half (ascending)
for (j = 1; j <= i; j++) completes the palindrome: 1, 2, ..., i.
Palindrome rows
Each row has \(2i-1\) digits, so total printed digits grow like O(n²).
Variation — User Input Rows
Let the user choose the number of rows at runtime.
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
Two loops per row build the palindrome: descending then ascending.
The center digit is always 1, so each row is symmetric.
Row \(i\) prints \(2i-1\) digits.
Total work grows as O(n²) for n rows.
❓ Frequently Asked Questions
4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.Console.Write(j + " ") in both inner loops. You can trim trailing spaces if needed.rows - i spaces (or a larger width if you add digit spacing).Explore More C# Number Patterns!
Learn more palindrome and symmetry-based patterns to strengthen your loop logic.
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\).
12 people found this page helpful
