C# Number Triangle Pattern (Alternating Direction)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

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.

How to Solve It

One nested loop, a shared counter, and an odd/even branch for print direction.

MethodIdeaBest for
Counter + parityOdd: print next; even: print end--Learning, interviews, exams
Rows inputSame logic with a user-chosen heightPractice / 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

GoalPattern
Init counterint next = 1;
Row end valueint end = next + i - 1;
Odd rowConsole.Write(next + " ");
Even rowConsole.Write(end-- + " ");
Advancenext++; once per printed value

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach number plus a trailing space
Console.WriteLineEnds the current lineAfter the inner loop finishes a row

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 result 5 rows · 15 numbers
1 
3 2 
4 5 6 
10 9 8 7 
11 12 13 14 15 

Worked Walkthrough — rows = 4

Trace next, end, and print direction on each row.

inext startDirectionPrinted row
11odd → ascend1
22even → end=33 2
34odd → ascend4 5 6
47even → end=1010 9 8 7

Total numbers for n rows = n(n+1)/2. Never reset next between rows.

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();
            }
        }
    }
}

How It Works

1. Counter. next starts at 1 and advances once per printed value — never reset.

2. Odd rows. Print next left-to-right (row 1: 1; row 3: 4 5 6).

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();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

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();
            }
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Increasing-Decreasing Number Pyramid

Print palindromic rows like 1, 232, 34543 with ascend then descend halves.

Program 52 tutorial →

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.

6 people found this page helpful