Shape Rule
Shrinking rows
Row i prints rows - i + 1 numbers from counter k — width shrinks each line.

The continuous number triangle prints 1 2 3 4 5, then 6 7 8 9, then 10 11 12, … — each row has one fewer number — a natural follow-up after Program 37’s palindrome triangle. This tutorial covers counter k, decreasing row width, fixed-width formatting, nested loops, a live preview, worked C# examples, edge cases, and complexity.
Shrinking rows
Row i prints rows - i + 1 numbers from counter k — width shrinks each line.
i = 1..rows
for (i = 1; i <= rows; i++) — one shrinking row per iteration.
rows..i
for (j = rows; j >= i; j--) — prints one fewer number each row.
{0,3} format
Console.Write("{0,3}", k++) — continuous sequence with fixed-width columns.
3–7 rows
Pick a row count and draw the decreasing-width continuous triangle in the browser.
Complexity
Total prints = n(n+1)/2 — work scales as n².
A continuous number triangle with decreasing row length prints numbers from a counter k: 1 2 3 4 5, then 6 7 8 9, then 10 11 12, and so on. With rows = 5, each row has one fewer number than the row above.
In C# you use nested loops with counter k: inner loop j = rows..i prints {0,3} with k++, then WriteLine().
It combines a continuous counter with a shrinking inner bound — a step after Program 37’s palindrome rows.
Continuous seq.
Shrinking width.
Fixed-width columns.
Follow Program 37; continue to Program 39 next.
In short: outer i = 1..rows, inner j = rows..i, {0,3} with k++, then WriteLine().
Given rows = 5, print a continuous number triangle with decreasing row length: counter k starts at 1, inner loop j = rows..i prints {0,3} with k++.
// rows = 5
// 1 2 3 4 5
// 6 7 8 9
//10 11 12
//13 14
//15 | Item | Type | Description |
|---|---|---|
rows | int | Triangle height — number of shrinking lines to print. |
i | int | Outer loop — current row (1 to rows). |
j | int | Inner loop — runs from rows down to i. |
k | int | Continuous counter — starts at 1, increments per printed number. |
k = 1
for i from 1 to rows:
for j from rows down to i:
print k in width 3; k++
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed rows | 1 2 3 4 5, 6 7 8 9, … | Learning and interviews |
| User-input rows | int.TryParse(...) | Configurable triangle size |
| Compact trace | rows = 3 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 1; i <= rows; i++) |
| Inner loop | for (j = rows; j >= i; j--) Console.Write("{0,3}", k++); |
| Counter | int k = 1; before both loops |
| End the row | Console.WriteLine(); |
| User input | int.TryParse(Console.ReadLine(), out rows) |
Same decreasing-width continuous triangle — different ways to control the row count.
i = 1..rowsOne shrinking row per iteration
k++Continuous sequence
j = rows..iOne fewer number each row
{0,3}Fixed-width columns
Reach for this pattern when teaching continuous counters, shrinking inner bounds, and formatted console output.
Natural follow-up — replaces palindrome rows with a continuous counter and shrinking row width.
Practice k++ with {0,3} before tackling larger pattern series.
Combine loops with ReadLine and TryParse for flexible row counts.
Compare Program 35 (right-aligned) and Program 39 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in counter logic, shrinking inner bounds, and O(n²) thinking.
Choose a row count between 3 and 7 and draw the decreasing-width continuous number triangle in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five rows of the decreasing-width continuous number triangle with counter k and {0,3} formatting.
rows = 5Hard-coded row count — ideal for first demos and screenshots.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j, k = 1;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
Console.Write("{0,3}", k++);
Console.WriteLine();
}
}
}
} When i = 2, the inner loop runs j = 5, 4, 3, 2 — four numbers starting from k = 6. When i = 5, only j = 5 runs — a single number 15.
Read the row count from the console instead of hard-coding 5.
Read rows from the console with safe parsing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter rows: ");
if (!int.TryParse(Console.ReadLine(), out int rows) || rows < 1) return;
int k = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
Console.Write("{0,3}", k++);
Console.WriteLine();
}
}
}
} Same counter and inner-loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.
Run with rows = 3 to trace every row on paper before scaling up.
rows = 3Same counter and shrinking inner loop with a smaller row count for quick tracing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int k = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= i; j--)
Console.Write("{0,3}", k++);
Console.WriteLine();
}
}
}
} Only rows changes from 5 to 3 — the counter and inner loop stay identical. Trace i = 1, 2, 3 on paper to see how each row prints one fewer number.
using System; brings in Console. Set k = 1 and loop variables i, j with rows = 5.
for (i = 1; i <= rows; i++) — ascending outer loop; one shrinking row per iteration.
for (j = rows; j >= i; j--) — prints rows - i + 1 numbers from counter k.
Console.Write("{0,3}", k++) — fixed-width columns; counter never resets.
Console.WriteLine() ends the row after the inner loop finishes.
Numbers per row = rows - i + 1 — total prints = n(n+1)/2; O(n²) time.
rows = 5Trace each outer-loop value of i, inner range j, and full row output.
i | Inner range (j) | Count | Numbers | Row output |
|---|---|---|---|---|
1 | 5..1 | 5 | 1–5 | 1 2 3 4 5 |
2 | 5..2 | 4 | 6–9 | 6 7 8 9 |
3 | 5..3 | 3 | 10–12 | 10 11 12 |
4 | 5..4 | 2 | 13–14 | 13 14 |
5 | 5..5 | 1 | 15 | 15 |
Numbers per row = rows - i + 1 — total prints = 1 + 2 + ... + n = n(n+1)/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: reset k = 1 inside the outer loop and watch the sequence restart each row.
Foundation for continuous-counter patterns with shrinking row width.
Example: compare with Program 35 (right-aligned) and Program 39 next.
Practice {0,3} fixed-width columns when numbers become two digits.
Example: change to {0,4} for wider columns on large row counts.
Inner loop stops at i instead of 1 — one fewer number each row.
Example: row i prints rows - i + 1 numbers from counter k.
Triangular totals make O(n²) concrete for beginners.
Example: count prints for rows = 5 — total is 5+4+3+2+1 = 15 = 5×6/2.
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, inner range j = rows..i, and k on paper for rows = 3 before coding.
Small habits that keep number-pattern code clean.
Declare k = 1 once before the outer loop — never reset inside unless you want per-row numbering.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Write j = rows..i and count rows - i + 1 numbers per row before coding.
Trace i = 1..3 and k on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put WriteLine inside the inner loop.
Mistakes that commonly break decreasing-width continuous number triangles.
Each number lands on its own line — you get a column, not a triangle.
→ Use Write("{0,3}", k++); WriteLine only after the inner loop.
Putting k = 1 inside the outer loop restarts the sequence on every row.
→ Declare k = 1 once before the outer loop; only increment with k++ when printing.
Using j >= 1 prints the same width every row — no shrinking effect.
→ Keep for (j = rows; j >= i; j--) — stop at i, not 1.
Without fixed-width formatting, two-digit numbers misalign columns.
→ Use Console.Write("{0,3}", k++) for aligned output.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one number from counter k.
Outer loop never runs when rows < 1 — print nothing or show a message.
rows < 1Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 2 and 3.
Convert.ToInt32 throws — use TryParse.
Total prints = n(n+1)/2 — grows quadratically with rows.
Try these variations to lock in the pattern.
i prints rows - i + 1 numbersj = rows..i, counter k never resetsTryParse until rows >= 1k = 1 before loops. Inner loop runs j = rows..i — print {0,3} with k++; row i prints rows - i + 1 numbers.Console.Write stays on the line; WriteLine advances — mix them carefully.rows >= 1 for interactive programs; rows = 1 prints a single 1.n(n+1)/2 — compare with Program 35 where each row grows instead of shrinking.Quick Takeaway: outer i = 1..rows, inner j = rows..i, {0,3} with k++, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The decreasing-width continuous number triangle is a compact lesson in counters and shrinking bounds: declare k = 1, inner loop j = rows..i, print {0,3} with k++, and end each row with WriteLine(). Master the fixed-rows version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 39 for the next pattern in the series.
Counter k must stay outside the outer loop — validate rows when reading from the console.
for (i = 1; i <= rows; i++) in the outer loopfor (j = rows; j >= i; j--) Console.Write("{0,3}", k++);int k = 1; before both loopsint.TryParse over bare Convert.ToInt32WriteLine inside the inner loopk = 1 inside the outer loopj >= 1 in the inner loop (no shrinking width)rows = 1 edge casePrint the pattern the beginner-friendly way.
Continuous seq.
DefinitionShrinking width
CodeFixed-width
CodeWriteLine after j
ShapeO(n²) time
AnalysisA counter k starts at 1 and increments every time a number prints. Row i prints rows - i + 1 numbers with {0,3} — total prints = n(n+1)/2.
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful