Column-Wise Number Triangle in C#

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + 2D Array

What You’ll Learn

How to print a triangle where numbers are filled column by column, then displayed row by row.

This is a fun pattern because it’s different from the usual left-to-right numbering: after filling, the second column becomes 6 7 8 9, the third becomes 10 11 12, and so on.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1\n2 6\n3 7 10\n4 8 11 13\n5 9 12 14 15
1

Complete C# Program

Fill the triangle column-wise using a small 2D array, then print each row up to its length.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int[,] tri = new int[rows + 1, rows + 1];

            int num = 1;
            for (int col = 1; col <= rows; col++)
            {
                for (int row = col; row <= rows; row++)
                {
                    tri[row, col] = num++;
                }
            }

            for (int row = 1; row <= rows; row++)
            {
                for (int col = 1; col <= row; col++)
                {
                    Console.Write(tri[row, col]);
                    if (col < row) Console.Write(" ");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Create storage for the triangle

int[,] tri = new int[rows+1, rows+1]; stores values so we can fill column-wise but print row-wise.

Setup
2

Fill column-wise

Loop by column first: col = 1..rows. For each column, fill rows row = col..rows with the next increasing number.

Filling
3

Print row-wise

Print row 1 with 1 value, row 2 with 2 values, and so on: col = 1..row.

Output
4

Understand the counts

Total printed numbers are \(1+2+\dots+rows = rows(rows+1)/2\), so the program is O(rows²).

Complexity
=

Column-wise triangle

Filling order (column first) is what makes the values look “jumped” on each row.

2

Variation — User Input Rows

Let the user choose the triangle size at runtime with input validation.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

            int[,] tri = new int[rows + 1, rows + 1];
            int num = 1;

            for (int col = 1; col <= rows; col++)
            {
                for (int row = col; row <= rows; row++)
                {
                    tri[row, col] = num++;
                }
            }

            for (int row = 1; row <= rows; row++)
            {
                for (int col = 1; col <= row; col++)
                {
                    Console.Write(tri[row, col]);
                    if (col < row) Console.Write(" ");
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Right-align the triangle using PadLeft for a cleaner look
  • Print each value with fixed width (useful for 2-digit and 3-digit numbers)
  • Fill row-wise instead of column-wise to compare patterns
  • Replace the numbers with characters for a column-wise alphabet triangle

Avoid

  • Using rows too large without formatting (alignment becomes messy)
  • Skipping input validation for user-entered sizes
  • Mixing fill order and print order without storing values (it gets confusing)

Key Takeaways

1

Fill the triangle column-wise to get the distinctive jumps (6, 10, 13…).

2

Store values in a small array so you can print cleanly row-by-row.

3

Total printed values are \(rows(rows+1)/2\).

4

Same concept works for other fill orders (row-wise, diagonal-wise, spiral, etc.).

❓ Frequently Asked Questions

Because the triangle is filled column-wise: after finishing the first column (1..5), the next available number is 6 for the second column.
Yes. Just loop rows first and columns inside, assigning increasing numbers as you print. The output becomes the standard 1, 2 3, 4 5 6… triangle.
Not strictly, but it keeps the logic clear: fill in one order, print in another.
O(rows²), because the triangle contains about rows(rows+1)/2 values.

Explore More C# Number Patterns!

Try changing the fill order to discover new, interesting triangle patterns.

All Number Patterns →
Did you know?

For any rows = n, the largest number printed is \(n(n+1)/2\) because the triangle contains exactly that many cells.

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