In C# use two inner loops: forward j from i to rows, then wrap k from i down to 2 printing k - 1.
Approach
How to Solve It
One outer loop and two inner loops that rotate the digit sequence.
Method
Idea
Best for
Forward + wrap
Print i..rows, then i-1..1
Learning, interviews, exams
User-input rows
Same logic with a variable width
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i to rows:
print j
for k from i down to 2:
print (k - 1)
print newline
Cheat sheet
Goal
Pattern
Next rotation
for (i = 1; i <= rows; i++)
Forward segment
for (j = i; j <= rows; j++) Console.Write(j);
Wrap segment
for (k = i; k > 1; k--) Console.Write(k - 1);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit (no spaces)
Console.WriteLine
Ends the current line
After both inner loops
Try it
Live Preview
Change the row count and the rotating pattern updates instantly — each row stays the same width.
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 · 5 digits/row
12345
23451
34521
45321
54321
Trace
Worked Walkthrough — rows = 4
Trace the forward climb and the wrap-around. Every row has exactly 4 digits.
i
Forward / wrap
Printed row
1
1234 / (none)
1234
2
234 / 1
2341
3
34 / 21
3421
4
4 / 321
4321
Forward length is rows - i + 1; wrap length is i - 1. Sum = rows.
Code
C# Programs
Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Forward loop prints i..rows; wrap loop prints i-1..1 via k - 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 <= rows; j++)
Console.Write(j);
for (k = i; k > 1; k--)
Console.Write(k - 1);
Console.WriteLine();
}
}
}
}
Output
12345
23451
34521
45321
54321
How It Works
1. Outer loop.i is the start digit of the rotation for that row.
2. Forward. Print from i up to rows — e.g. row 3 prints 345.
3. Wrap. Print k - 1 while k runs from i down to 2 — completing 34521.
Example 2 — User Input
Read the row count; each row still prints exactly rows digits.
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 = i; j <= rows; j++)
Console.Write(j);
for (k = i; k > 1; k--)
Console.Write(k - 1);
Console.WriteLine();
}
}
}
}
2. Same rotation. Three rows of width 3 end at 321.
Example 3 — Compact rows = 3
A smaller fixed demo — same forward-and-wrap idea, 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 <= rows; j++)
Console.Write(j);
for (k = i; k > 1; k--)
Console.Write(k - 1);
Console.WriteLine();
}
}
}
}
Output
123
231
321
How It Works
1. Same rules. Forward prints i..rows; wrap prints i-1..1.
2. Quick check. The last row is a full reverse: 321.
Edge Cases & Pitfalls
Check these before calling the solution done.
print k
Print k instead of k - 1
The wrap would reprint i and skip 1. Always print k - 1.
wrong wrap end
Run wrap while k >= 1
That prints an extra 0. Keep k > 1.
WriteLine early
WriteLine between the two halves
That splits forward and wrap onto separate lines. Call WriteLine() only after both loops.
rows = 1
Single 1
Only the forward loop runs; the wrap loop never executes.
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
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
There are n rows and each prints exactly n digits, so total work is n².
Remember
Key Takeaways
Rule: print i..rows, then i-1..1 — each row has exactly rows digits.
Wrap: use Console.Write(k - 1) while k runs from i down to 2.
Write vs WriteLine: digits stay on the line; WriteLine advances after both halves.
Next step: Program 40 prints an alternating 1 and 0 pattern.
One line: for each row i, print a forward climb to rows and a wrap back to 1 so the sequence rotates.
Frequently Asked Questions
For 5 rows: 12345, 23451, 34521, 45321, 54321 — each row starts at the row number and wraps back to 1.
The first loop prints i..rows (forward segment). The second loop prints i-1 down to 1 (wrap segment). Together they always produce rows digits.
After printing 2 3 4 5, the wrap loop prints k-1 when k runs from i down to 2 — for i=2 that prints 1.
Exactly rows digits every time — (rows - i + 1) forward plus (i - 1) wrap = rows.
Program 37 builds palindrome rows (i..2 then 1..i). Program 39 rotates: i..rows then i-1..1.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because each row prints n digits.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single digit 1.
🤔
Did you know?
Each row starts at i, prints i..rows, then wraps with i-1..1. Row i always prints exactly rows digits — total digits = n².