In C# use one outer loop and two inner loops. Every row prints exactly rows digits — left half shrinks as the right half grows.
Approach
How to Solve It
One outer row loop, then descending and ascending half-loops.
Method
Idea
Best for
Two-half row
Descend i..2, then ascend 1..(rows-i+1)
Learning, interviews, exams
Rows input
Same logic with a user-chosen width
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down while j > 1:
print j
for k from 1 to (rows - i + 1):
print k
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (i = 1; i <= rows; i++)
Descending half
for (j = i; j > 1; j--) Console.Write(j);
Ascending half
for (k = 1; k <= rows + 1 - i; k++) Console.Write(k);
End the row
Console.WriteLine();
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 half-loops finish a row
Try it
Live Preview
Change the row count and the mixed triangle updates instantly.
Whole numbers from 1 to 9 (keeps single-digit rows). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
12345
21234
32123
43212
54321
Trace
Worked Walkthrough — rows = 4
Trace descending and ascending halves on each row.
i
Descend / ascend
Printed row
1
(none) / 1234
1234
2
2 / 123
2123
3
32 / 12
3212
4
432 / 1
4321
Each row has rows digits. Total prints for n rows = n².
Code
C# Programs
Three complete programs: fixed 5 rows, user-input rows, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Two inner loops: descend i..2, then ascend 1..(rows-i+1).
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 = i; j > 1; j--)
Console.Write(j);
for (k = 1; k <= rows + 1 - i; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
Output
12345
21234
32123
43212
54321
How It Works
1. Outer loop.i picks the row — larger i means a longer descending half.
2. Descend. Print j from i down while j > 1 (skipped when i = 1).
3. Ascend. Print k from 1 to rows + 1 - i so the row stays width rows.
Example 2 — User Input (rows)
Read the row count and build the same mixed triangle.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, i, j, k;
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++)
{
for (j = i; j > 1; j--)
Console.Write(j);
for (k = 1; k <= rows + 1 - i; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
2. Same two halves. Only the width changes from the literal 5.
Example 3 — Compact rows = 3
A smaller fixed demo — easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, j, k;
for (i = 1; i <= rows; i++)
{
for (j = i; j > 1; j--)
Console.Write(j);
for (k = 1; k <= rows + 1 - i; k++)
Console.Write(k);
Console.WriteLine();
}
}
}
}
Output
123
212
321
How It Works
1. Same rules. Descend then ascend; each row stays three digits wide.
2. Quick check. Row 1 is all ascending; row 3 is almost all descending ending in 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
j >= 1
Descend to 1 inclusive
That doubles the digit 1 with the ascending half. Keep j > 1 (or j >= 2).
wrong bound
Ascending to rows every time
Rows get too long. Use rows + 1 - i so width stays constant.
WriteLine
WriteLine inside a half-loop
That breaks the row. Call WriteLine only after both inner loops.
rows = 1
Single row
Output is just 1 — descending is skipped.
spaces
Add spaces between digits
Fine for readability — use Console.Write(j + " ") — but the classic pattern is tight digits.
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)
Each of n rows prints exactly n digits, so total work is n².
Remember
Key Takeaways
Rule: descend i..2, then ascend 1..(rows-i+1).
Width: every row has exactly rows digits — left grows as right shrinks.
Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
Next step: Program 51 alternates ascending and descending rows with a running counter.
One line: for each row, print a descending prefix then an ascending suffix that keep the width fixed.
Frequently Asked Questions
Each row prints two parts: descending from i down to 2, then ascending from 1 up to (rows - i + 1). Row 2 becomes 2 + 1234 = 21234.
One prints the descending half (j = i down while j > 1). The other prints the ascending half (k = 1..(rows-i+1)). Splitting them keeps both halves clear.
When i = 1, the descending loop never runs. Only the ascending loop prints 1..rows — a full ascending line.
The descending loop prints i first. For i = 2 that is 2, then the ascending loop prints 1..4.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because each row prints n digits — total prints = n².
Program 49 prints i*j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
Both work for the descending loop and print the same i..2 sequence.
One row prints 1 — the descending loop is skipped and the ascending loop prints only 1.
🤔
Did you know?
Each row combines two sequences: descending i..2, then ascending 1..(rows-i+1). Row 2 prints 21234; row 5 prints 54321. Each row has rows digits — total prints = n².