Alternating Number Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Odd/Even Rows

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:

Output
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
1

Complete C# Program

We keep a running counter for the next number to print, and reverse the print direction on even rows.

C#
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

1

Track the next number

next stores the next number to print and increments once per output value.

Counter
2

Outer loop controls the row size

for (i = 1; i <= rows; i++) prints row i with exactly i numbers.

Row control
3

Compute the row’s end value

end = next + i - 1 gives the last number that belongs to the current row.

Row math
4

Alternate printing direction

Odd rows print next (ascending). Even rows print end-- (descending).

Odd/Even
=

Alternating triangle

Total printed numbers are \(1+2+\dots+n = n(n+1)/2\), so time complexity is O(n²).

2

Variation — User Input Version

Read rows from the user and validate it using int.TryParse:

C#
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 next to 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 end for even rows
  • Skipping input validation when accepting rows from the user

Key Takeaways

1

A single running counter keeps the numbers continuous across rows.

2

Odd/even row checks (i % 2) control the printing direction.

3

The row-end value can be computed as next + i - 1 before printing the row.

4

This alternating technique is useful in many zig-zag style patterns.

❓ Frequently Asked Questions

Row 2 is an even row, so it prints in reverse order. The row contains numbers 2 and 3, but they are printed as 3 2.
Before printing row i, the last number is next + i - 1. That gives the correct starting point when printing the row in reverse.
Yes. Build a row string (e.g., with StringBuilder) and trim the final space before printing.
O(n²) for n rows, because the total prints are 1+2+…+n.

Explore More C# Number Patterns!

Alternating direction patterns are a fun way to level up your loop control.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful