C# Number Triangle Pattern (Alternating Direction)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An alternating ascending/descending triangle prints continuous numbers: odd rows left-to-right, even rows right-to-left.
Remember
Rule: odd rows print next ascending; even rows print end descending
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15 ← rows = 5
In C# keep a running counter next. On even rows set end = next + i - 1 and print while decrementing end.
Approach
How to Solve It
One nested loop, a shared counter, and an odd/even branch for print direction.
Method
Idea
Best for
Counter + parity
Odd: print next; even: print end--
Learning, interviews, exams
Rows input
Same logic with a user-chosen height
Practice / demos
Pseudocode
Pseudocode
next = 1
for i from 1 to rows:
end = next + i - 1
repeat i times:
if i is odd: print next
else: print end, then end = end - 1
next = next + 1
print newline
Cheat sheet
Goal
Pattern
Init counter
int next = 1;
Row end value
int end = next + i - 1;
Odd row
Console.Write(next + " ");
Even row
Console.Write(end-- + " ");
Advance
next++; once per printed value
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each number plus a trailing space
Console.WriteLine
Ends the current line
After the inner loop finishes a row
Try it
Live Preview
Change the row count and the alternating triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 numbers
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
Trace
Worked Walkthrough — rows = 4
Trace next, end, and print direction on each row.
i
next start
Direction
Printed row
1
1
odd → ascend
1
2
2
even → end=3
3 2
3
4
odd → ascend
4 5 6
4
7
even → end=10
10 9 8 7
Total numbers for n rows = n(n+1)/2. Never reset next between rows.
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
Odd rows print next ascending; even rows print end-- descending.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int next = 1;
int i, j, end;
for (i = 1; i <= rows; i++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
}
Output
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
How It Works
1. Counter.next starts at 1 and advances once per printed value — never reset.
3. Even rows. Set end = next + i - 1 and print while decrementing (row 2: 3 2).
Example 2 — User Input (rows)
Read the row count and build the same zig-zag triangle.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, next = 1, i, j, end;
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++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
}
2. Same zig-zag. Only the height changes from the literal 5.
Example 3 — Compact rows = 3
A smaller fixed demo — easy to trace every next step by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int next = 1;
int i, j, end;
for (i = 1; i <= rows; i++)
{
end = next + i - 1;
for (j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
}
Output
1
3 2
4 5 6
How It Works
1. Same rules. Odd ascend, even descend; next never resets.
2. Quick check. After row 2, next is 4 — so row 3 prints 4 5 6.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset next
Reset next = 1 each row
Numbering restarts every line. Keep next outside the outer loop.
wrong end
Forget end = next + i - 1
Even rows print the wrong reverse sequence. Compute end at the start of each row.
no next++
Skip incrementing next on even rows
Later rows reuse old numbers. Increment next on every print, both directions.
WriteLine
WriteLine inside the inner loop
That puts every number on its own line. Call WriteLine only after the column loop.
rows = 1
Single row
Output is just 1 — one ascending value.
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)
Total printed numbers are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the row count.
Remember
Key Takeaways
Rule: odd rows ascend with next; even rows descend from end.
Counter: keep next outside the outer loop; set end = next + i - 1 per row.
Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
Next step: Program 52 prints an increasing-decreasing number pyramid (1, 232, 34543…).
One line: keep a continuous counter; print forward on odd rows and backward on even rows.
Frequently Asked Questions
Row 2 is even, so it prints in reverse. The row uses numbers 2 and 3, printed as 3 2 by decrementing end.
Even rows print right-to-left for the zig-zag. Compute end = next + i - 1 and decrement while printing.
A running counter next increments once per printed value and is never reset between rows.
Before printing row i, the last number is next + i - 1 — the start point when printing in reverse.
Change rows or read it from user input with TryParse — see Example 2.
O(n²) for n rows because total prints are 1+2+…+n = n(n+1)/2.
Program 50 concatenates fixed digit sequences per row. Program 51 uses a continuous counter and alternates print direction on odd/even rows.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
One row prints 1 — a single ascending value on the first odd row.
🤔
Did you know?
Numbers stay continuous via a running counter next. Odd rows print ascending; even rows print descending with end = next + i - 1. Total prints = n(n+1)/2.