Column-Wise Number Triangle in C#

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:
1\n2 6\n3 7 10\n4 8 11 13\n5 9 12 14 15Complete C# Program
Fill the triangle column-wise using a small 2D array, then print each row up to its length.
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
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.
Fill column-wise
Loop by column first: col = 1..rows. For each column, fill rows row = col..rows with the next increasing number.
Print row-wise
Print row 1 with 1 value, row 2 with 2 values, and so on: col = 1..row.
Understand the counts
Total printed numbers are \(1+2+\dots+rows = rows(rows+1)/2\), so the program is O(rows²).
Column-wise triangle
Filling order (column first) is what makes the values look “jumped” on each row.
Variation — User Input Rows
Let the user choose the triangle size at runtime with input validation.
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
PadLeftfor 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
rowstoo 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
Fill the triangle column-wise to get the distinctive jumps (6, 10, 13…).
Store values in a small array so you can print cleanly row-by-row.
Total printed values are \(rows(rows+1)/2\).
Same concept works for other fill orders (row-wise, diagonal-wise, spiral, etc.).
❓ Frequently Asked Questions
rows(rows+1)/2 values.Explore More C# Number Patterns!
Try changing the fill order to discover new, interesting triangle patterns.
For any rows = n, the largest number printed is \(n(n+1)/2\) because the triangle contains exactly that many cells.
12 people found this page helpful
