C# Incremental Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned incremental triangle pads each row with spaces, then prints the next values from a continuous counter k that never resets between rows.
In C# the inner loop always runs from rows down to 1. Print " " when j > i; otherwise Console.Write("{0,3}", k++).
Approach
How to Solve It
One fixed-width loop with a shared counter and format width.
Method
Idea
Best for
Counter + spaces
Pad left; print k++ with {0,3}
Learning, interviews, exams
User-input rows
Same logic with a variable height
Practice / demos
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print three spaces
else: print k (width 3), then k = k + 1
print newline
Cheat sheet
Goal
Pattern
Grow filled width
for (i = 1; i <= rows; i++)
Fixed column scan
for (j = rows; j >= 1; j--)
Leading pad
if (j > i) Console.Write(" ");
Next number
Console.Write("{0,3}", k++);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each space group or number
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the row count and the right-aligned counter triangle updates instantly.
Whole numbers from 1 to 8 (width-3 columns stay readable). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 values
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 4
Trace spaces vs numbers. Counter k keeps climbing across rows.
i
Spaces / numbers
Printed row
1
3 pads / 1
1
2
2 pads / 2 3
2 3
3
1 pad / 4 5 6
4 5 6
4
none / 7..10
7 8 9 10
Each row is always 3 × rows characters wide — space groups and formatted numbers combined.
Code
C# Programs
Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Fixed-width scan: print three spaces while j > i, otherwise print and advance k.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j, k = 1;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,3}", k++);
}
Console.WriteLine();
}
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How It Works
1. Outer loop.i grows from 1 to 5 — each pass fills one more number slot on the right.
2. Inner loop.j scans from 5 down to 1. Spaces while j > i; otherwise print k++ in width 3.
3. Continuous k.k lives outside the loops, so the sequence runs 1…15 without resetting.
Example 2 — User Input
Read the row count; the inner loop uses rows as the fixed width.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j, k = 1;
Console.Write("Enter 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++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,3}", k++);
}
Console.WriteLine();
}
}
}
}
2. Same alignment. Three rows print values 1 through 6 with width-3 columns.
Example 3 — Compact rows = 3
A smaller fixed demo — same counter idea, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, j, k = 1;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write("{0,3}", k++);
}
Console.WriteLine();
}
}
}
}
Output
1
2 3
4 5 6
How It Works
1. Same rules. Spaces while j > i; otherwise print the next k in width 3.
2. Full width. The last row has no leading pads — just 4 5 6.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset k
Reset k = 1 each row
That reprints 1, 2, 3… on every row. Keep k outside both loops.
wrong pad
Print one space instead of three
Columns drift once numbers hit two digits. Match the pad width to {0,3}.
no format
Use Write(k++) without width
Alignment breaks at 10+. Prefer Console.Write("{0,3}", k++).
rows = 1
Single 1
Output is just the formatted 1 — no leading space groups.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
There are n rows; each runs a fixed-width inner loop of n steps, so work is n². Printed numbers alone are n(n+1)/2.
Remember
Key Takeaways
Rule: spaces while j > i; otherwise print continuous k++ in width 3.
Fixed width: the inner loop always runs rows times so columns stay right-aligned.
Write vs WriteLine: pads and numbers stay on the line; WriteLine advances after each row.
Next step: Program 36 prints a right-aligned decreasing number triangle.
One line: for each row, pad with spaces then print the next values from a shared counter using fixed column width.
Frequently Asked Questions
Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1 with no leading space groups.
🤔
Did you know?
A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and {0,3} keeps columns aligned as values grow past single digits.