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.
Approach
How to Solve It
One two-loop idea with two useful variants — packed digits and spaced digits.
Method
Idea
Best for
Ascend + mirror
Print 1..i, then i-1..1 with Write
Learning, interviews, exams
Spaced digits
Same 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
Goal
Pattern
Grow the row
for (i = 1; i <= rows; i++)
Left half
for (j = 1; j <= i; j++) Console.Write(j);
Mirror half
for (k = i - 1; k >= 1; k--) Console.Write(k);
End the row
Console.WriteLine();
Add spaces
Console.Write(j + " "); in both loops
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit j or k
Console.WriteLine
Ends the current line
After both inner loops
Try it
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 result5 rows · 25 digits
1
121
12321
1234321
123454321
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i: the ascending half, the mirror half, and the full row.
i
Ascend 1..i
Mirror i-1..1
Printed row
1
1
—
1
2
12
1
121
3
123
21
12321
4
1234
321
1234321
Total digits are 1+3+5+7 = 16 — equal to rows² for any rows.
Code
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();
}
}
}
}
Output
1
121
12321
1234321
123454321
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();
}
}
}
}
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();
}
}
}
}
Output
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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².
Remember
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.