An alternating zigzag number triangle shrinks each row while flipping print direction: odd length prints 1..i, even length prints i..1.
Remember
Rule: for i = n..1, odd → 1..i, even → i..1
12345
4321
123
21
1 ← 5 rows
In C# you solve it with a descending outer loop plus if (i % 2 == 0): even rows use a descending inner loop, odd rows use ascending, then WriteLine() ends the row. Program 12 is a repeating-digit shrink; this one prints sequential digits and zigzags.
Approach
How to Solve It
Two ways to emit the same shape — start with if/else branches, then optionally collapse them into start/end/step variables.
Method
Idea
Best for
If/else + nested loops
Outer shrinks n..1; parity picks ascending or descending j
Learning, interviews, exams
Direction variables
One inner loop with start, end, and step
Shorter demos once parity clicks
Pseudocode
Pseudocode
for i from rows down to 1:
if i is even:
for j from i down to 1:
print j (no newline)
else:
for j from 1 to i:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Shrink each row
for (i = rows; i >= 1; i--)
Odd → ascending
for (j = 1; j <= i; j++) Console.Write(j);
Even → descending
for (j = i; j >= 1; j--) Console.Write(j);
Pick branch
if (i % 2 == 0) { ... } else { ... }
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit j
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the row count and the zigzag triangle updates instantly — including the total digit count.
Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
12345
4321
123
21
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, its parity, and the digits printed.
i
Parity
Direction
Printed row
4
even
4..1
4321
3
odd
1..3
123
2
even
2..1
21
1
odd
1..1
1
Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.
Code
C# Programs
Three complete programs: fixed height, row-count input, and a direction-variables shortcut. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop shrinks the length; i % 2 picks ascending or descending digits.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
if (i % 2 == 0)
{
for (j = i; j >= 1; j--)
{
Console.Write(j);
}
}
else
{
for (j = 1; j <= i; j++)
{
Console.Write(j);
}
}
Console.WriteLine();
}
}
}
}
Output
12345
4321
123
21
1
How It Works
1. Shrink.for (i = rows; i >= 1; i--) yields 5, 4, 3, 2, 1 — the length of each row.
2. Flip direction. Odd i prints 1..i (12345); even i prints i..1 (4321).
3. Newline.WriteLine() after the inner loop starts the next shorter zigzag row.
Example 2 — User Input
Read the row count at runtime. Prefer int.TryParse in real apps so bad input does not throw.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
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 = rows; i >= 1; i--)
{
if (i % 2 == 0)
{
for (j = i; j >= 1; j--)
{
Console.Write(j);
}
}
else
{
for (j = 1; j <= i; j++)
{
Console.Write(j);
}
}
Console.WriteLine();
}
}
}
}
2. Same zigzag logic. Four rows start at 4321 (even) and end at 1 (odd).
Example 3 — Direction Variables
Replace the if/else blocks with start, end, and step derived from parity.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
for (int i = rows; i >= 1; i--)
{
int start = (i % 2 == 0) ? i : 1;
int end = (i % 2 == 0) ? 1 : i;
int step = (i % 2 == 0) ? -1 : 1;
for (int j = start; step > 0 ? j <= end : j >= end; j += step)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}
Output
12345
4321
123
21
1
How It Works
1. Encode direction. Even i sets start = i, end = 1, step = -1; odd sets the opposite.
2. One inner loop. The condition step > 0 ? j <= end : j >= end walks either way — same output as Example 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
swapped parity
Ascending on even rows
That mirrors the zigzag. Keep odd → 1..i and even → i..1 for this lesson’s sample output.
WriteLine early
WriteLine(j) inside the inner loop
That puts each digit on its own line. Use Write(j); call WriteLine() only after the row finishes.
no shrink
for (i = 1; i <= rows; i++)
Counting up grows the triangle. Keep i = rows; i >= 1; i-- so the first line is longest.
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
rows > 9
Write(10) prints two characters, so columns drift. Labs usually stick to 1–9.
Analysis
Time and Space Complexity
Program
Time
Extra space
If/else nested loops (Examples 1–2)
O(n²)
O(1)
Direction variables (Example 3)
O(n²)
O(1)
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = n..1, odd prints 1..i, even prints i..1.
Parity:i % 2 == 0 is the flip switch that creates the zigzag.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 14 builds an odd-length descending number triangle.
One line: shrink i from n to 1; use parity to print 1..i or i..1 with Write, then WriteLine.
Frequently Asked Questions
The program checks whether i is even or odd. For odd i it prints 1..i ascending; for even i it prints i..1 descending, creating a zigzag direction.
The second row from the top has length i = 4, which is even. The even branch runs for (j = i; j >= 1; j--) and prints 4, 3, 2, 1.
Console.Write stays on the same line. Console.WriteLine ends the current line. Digits use Write(j); the row break uses WriteLine after the inner loop.
Remove the if/else and keep only one inner loop — either 1..i for ascending or i..1 for descending on every row.
Program 12 repeats the row digit with for (j = i; j <= rows; j++) (11111, 2222, 333, 44, 5). Program 13 prints sequential digits 1..i or i..1 and alternates direction using i % 2.
O(n²) where n is the number of rows. Total Console.Write calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. Swap the if and else branches so odd rows descend and even rows ascend — the zigzag still works, just mirrored.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Odd row length i prints 1..i; even row length prints i..1. The outer loop shrinks from rows to 1, and i % 2 flips direction — still O(n²) total digit prints.