Alternating Number Triangle in C#

What You’ll Learn
How to print an alternating number triangle in C# where odd rows print left-to-right and even rows print right-to-left.
The numbers are continuous across rows: they don’t restart at 1 on each line. This makes the problem a good exercise for loop bounds, counters, and row parity.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15Complete C# Program
We keep a running counter for the next number to print, and reverse the print direction on even rows.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int next = 1;
for (int i = 1; i <= rows; i++)
{
int end = next + i - 1;
for (int j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Track the next number
next stores the next number to print and increments once per output value.
Outer loop controls the row size
for (i = 1; i <= rows; i++) prints row i with exactly i numbers.
Compute the row’s end value
end = next + i - 1 gives the last number that belongs to the current row.
Alternate printing direction
Odd rows print next (ascending). Even rows print end-- (descending).
Alternating triangle
Total printed numbers are \(1+2+\dots+n = n(n+1)/2\), so time complexity is O(n²).
Variation — User Input Version
Read rows from the user and validate it using int.TryParse:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
int next = 1;
for (int i = 1; i <= rows; i++)
{
int end = next + i - 1;
for (int j = 1; j <= i; j++)
{
if (i % 2 == 1)
Console.Write(next + " ");
else
Console.Write(end-- + " ");
next++;
}
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Remove the trailing space by building a row string and trimming it
- Align numbers in columns using fixed-width formatting for larger values
- Start from a different number by initializing
nextto something else - Flip the parity rule so odd rows print descending instead
Avoid
- Resetting the counter each row (that breaks the continuous numbering)
- Forgetting to compute
endfor even rows - Skipping input validation when accepting
rowsfrom the user
Key Takeaways
A single running counter keeps the numbers continuous across rows.
Odd/even row checks (i % 2) control the printing direction.
The row-end value can be computed as next + i - 1 before printing the row.
This alternating technique is useful in many zig-zag style patterns.
❓ Frequently Asked Questions
3 2.i, the last number is next + i - 1. That gives the correct starting point when printing the row in reverse.StringBuilder) and trim the final space before printing.Explore More C# Number Patterns!
Alternating direction patterns are a fun way to level up your loop control.
This is a small example of a zig-zag traversal: you print forward on one row and backward on the next. Similar logic appears in matrix problems and serpentine traversals.
12 people found this page helpful
