C# Palindrome Number Pattern (Increasing-Decreasing)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An increasing-decreasing pyramid prints each row as a palindrome: count up from i to the peak 2i - 1, then back down to i. Row i always has 2i - 1 digits.
Remember
Rule: m = i each row
print ascending i..(2i-1) with m++
m = m - 2 ← skip the peak
print descending with m--
1
232
34543
4567654
567898765 ← rows = 5
In C# set m = i each row, print the increasing half with Console.Write(m++), step back with m = m - 2, then print the decreasing half with Console.Write(m--) before WriteLine().
Approach
How to Solve It
Set m = i each row. Print the ascending half with m++, step back with m = m - 2, then print the descending half with m--.
Method
Idea
Best for
Two halves + step-back
Ascend with m++, m -= 2, descend with m--
Learning, interviews, exams
Rows input
Same logic with a user-chosen height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
m = i
for j from 1 to i:
print m, then m = m + 1
m = m - 2
for k from 1 to (i - 1):
print m, then m = m - 1
print newline
Cheat sheet
Goal
Pattern
Pick each row
for (i = 1; i <= rows; i++)
Start value
m = i;
Ascending half
for (j = 1; j <= i; j++) Console.Write(m++);
Skip the peak
m = m - 2;
Descending half
for (k = 1; k < i; k++) Console.Write(m--);
Digits on row i
2 * i - 1
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit in both halves
Console.WriteLine
Ends the current line
After both inner loops finish a row
Try it
Live Preview
Change the row count and the pyramid updates instantly — capped at 5 so every digit stays a single character (peak ≤ 9).
Whole numbers from 1 to 5. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 25 digits
1
232
34543
4567654
567898765
Trace
Worked Walkthrough — Row i = 3
Trace both halves for row 3 — ascending 345, step back, then descending 43.
Step
State
Prints
Start
m = 3
—
Ascend (3 times)
m++ prints 3, 4, 5
345
After ascend
m = 6, then m = m - 2 → 4
—
Descend (2 times)
m-- prints 4, then 3
43
Full row: 34543. Digits on row i = 2i - 1; total = n² → O(n²).
Code
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 — ascend with m++, step back with m = m - 2, descend with m--.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j, k, m;
for (i = 1; i <= rows; i++)
{
m = i;
for (j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
}
Output
1
232
34543
4567654
567898765
How It Works
1. Start each row. Set m = i so row 3 begins at 3, row 5 at 5.
2. Ascend to the peak. Print i digits with m++ — up to 2i - 1.
3. Step back, then descend.m = m - 2 skips the peak; the second loop prints i - 1 digits going down.
Example 2 — User Input (rows)
Read the row count and build the same palindromic pyramid.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, i, j, k, m;
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++)
{
m = i;
for (j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
}
2. Same core. Only the height changes from the literal 5 — both halves stay identical.
3. Single-digit tip. Cap demos so the peak stays a single digit (2 * rows - 1 ≤ 9), i.e. rows ≤ 5.
Example 3 — Compact rows = 3
Same two-half structure with a smaller height for quick paper tracing.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, j, k, m;
for (i = 1; i <= rows; i++)
{
m = i;
for (j = 1; j <= i; j++)
Console.Write(m++);
m = m - 2;
for (k = 1; k < i; k++)
Console.Write(m--);
Console.WriteLine();
}
}
}
}
Output
1
232
34543
How It Works
1. Three rows. Row 1 skips descending; row 2 prints 232; row 3 prints 34543.
2. Trace on paper. Confirm m = m - 2 after the peak so the middle digit is not doubled.
Edge Cases & Pitfalls
Check these before calling the solution done.
m - 1
Doubled peak
If you use m = m - 1 (or skip the step-back), the peak digit prints twice. Keep m = m - 2.
k <= i
Extra descending digit
The descending loop must run i - 1 times: for (k = 1; k < i; k++). Using k <= i adds one too many.
WriteLine
Broken rows
If WriteLine sits inside either half, each digit lands on its own line. Call it only after both loops.
rows = 1
Single row
Output is just 1 — the descending loop never runs. A good sanity check.
rows > 5
Multi-digit values
Past 5, the peak exceeds 9 and values like 10 break the tight look. Cap demos at 5 or use spaced / fixed-width format.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Row i prints 2i - 1 digits. Total = 1 + 3 + 5 + … + (2n - 1) = n². For n = 5 that is 25 digits.
Remember
Key Takeaways
Palindrome row: ascend i..(2i-1), then descend back to i.
Skip the peak: after ascending, m = m - 2 so the middle digit is not printed twice.
Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
Next step: Program 53 prints a mirror diagonal (V-shaped) number pattern.
One line:m = i; print ascending with m++, m -= 2, print descending with m--, then WriteLine().
Frequently Asked Questions
Row 3 starts at 3, prints up to 5 (345), then prints back down to 3 (43) after m = m - 2 — producing 34543.
Each row counts up from i to the peak 2i-1, then counts back down to i. The sequence reads the same left-to-right on each line.
After the increasing loop, m is one past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop does not repeat the peak digit.
Step back with m = m - 2 before the decreasing loop. The decreasing loop then runs i-1 times, skipping the peak.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because row i prints 2i-1 digits and 1+3+5+...+(2n-1) = n² total prints.
Program 51 uses a continuous counter with alternating direction across rows. Program 52 resets m = i each row and builds a palindromic line per row.
Yes. Print with Console.Write(m++ + " ") in both loops and trim trailing space if needed.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
One row prints 1 — the decreasing loop k < i never runs when i = 1.
🤔
Did you know?
Each row is palindromic: print i..(2i-1) ascending, then back down with m = m - 2 to skip the peak. Row 3 prints 34543 — total digits = 1+3+5+…+(2n-1) = n² for n rows.