Continuous Numbers with Decreasing Row Length in C#

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Counter + Formatting

What You’ll Learn

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.

Shape Rule

Shrinking rows

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

Outer Loop

i = 1..rows

for (i = 1; i <= rows; i++) — one shrinking row per iteration.

Inner Loop (j)

rows..i

for (j = rows; j >= i; j--) — prints one fewer number each row.

Counter k

{0,3} format

Console.Write("{0,3}", k++) — continuous sequence with fixed-width columns.

Live Preview

3–7 rows

Pick a row count and draw the decreasing-width continuous triangle in the browser.

O(n²)

Complexity

Total prints = n(n+1)/2 — work scales as .

Introduction

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().

Why it matters?

It combines a continuous counter with a shrinking inner bound — a step after Program 37’s palindrome rows.

Key Highlights

Counter k

Continuous seq.

rows..i

Shrinking width.

{0,3}

Fixed-width columns.

Series Foundation

Follow Program 37; continue to Program 39 next.

In short: outer i = 1..rows, inner j = rows..i, {0,3} with k++, then WriteLine().

📝 Problem & Approach

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

C#
// rows = 5
// 1  2  3  4  5
// 6  7  8  9
//10 11 12
//13 14
//15

Inputs & Outputs

ItemTypeDescription
rowsintTriangle height — number of shrinking lines to print.
iintOuter loop — current row (1 to rows).
jintInner loop — runs from rows down to i.
kintContinuous counter — starts at 1, increments per printed number.

Minimal workflow

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to i:
        print k in width 3; k++
    print newline

Approach comparison

ApproachIdeaBest for
Fixed rows1 2 3 4 5, 6 7 8 9, …Learning and interviews
User-input rowsint.TryParse(...)Configurable triangle size
Compact tracerows = 3 on paper firstDebugging loop bounds

⚡ Quick Reference

GoalPattern
Outer loopfor (i = 1; i <= rows; i++)
Inner loopfor (j = rows; j >= i; j--) Console.Write("{0,3}", k++);
Counterint k = 1; before both loops
End the rowConsole.WriteLine();
User inputint.TryParse(Console.ReadLine(), out rows)

📋 Fixed vs User Input vs Compact Demo

Same decreasing-width continuous triangle — different ways to control the row count.

Outer loop
i = 1..rows

One shrinking row per iteration

Counter
k++

Continuous sequence

Inner loop
j = rows..i

One fewer number each row

Learning tip
{0,3}

Fixed-width columns

Context

When This Pattern Shows Up

Reach for this pattern when teaching continuous counters, shrinking inner bounds, and formatted console output.

  1. After Program 37

    Natural follow-up — replaces palindrome rows with a continuous counter and shrinking row width.

  2. Counter + formatting drills

    Practice k++ with {0,3} before tackling larger pattern series.

  3. Console I/O practice

    Combine loops with ReadLine and TryParse for flexible row counts.

  4. Gateway to variants

    Compare Program 35 (right-aligned) and Program 39 (next in series) next.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Choose a row count between 3 and 7 and draw the decreasing-width continuous number triangle in the browser.

Try 3, 5, or 7. Rows between 3 and 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of the decreasing-width continuous number triangle with counter k and {0,3} formatting.

Example 1 — Fixed rows = 5

Hard-coded row count — ideal for first demos and screenshots.

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

How It Works

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.

📈 User Input

Read the row count from the console instead of hard-coding 5.

Example 2 — User input rows

Read rows from the console with safe parsing.

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

How It Works

Same counter and inner-loop core as Example 1; only rows comes from user input instead of being hard-coded as 5.

⚡ Smaller Demo

Run with rows = 3 to trace every row on paper before scaling up.

Example 3 — Compact rows = 3

Same counter and shrinking inner loop with a smaller row count for quick tracing.

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

How It Works

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.

🧠 How the Algorithm Prints Rows

1

Set up

using System; brings in Console. Set k = 1 and loop variables i, j with rows = 5.

Setup
2

Outer loop walks rows

for (i = 1; i <= rows; i++) — ascending outer loop; one shrinking row per iteration.

Row
3

Inner loop (shrinking width)

for (j = rows; j >= i; j--) — prints rows - i + 1 numbers from counter k.

Numbers
4

Print with formatting

Console.Write("{0,3}", k++) — fixed-width columns; counter never resets.

Format
5

New line

Console.WriteLine() ends the row after the inner loop finishes.

Break
=

Decreasing-width triangle complete

Numbers per row = rows - i + 1 — total prints = n(n+1)/2; O(n²) time.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value of i, inner range j, and full row output.

iInner range (j)CountNumbersRow output
15..151–51 2 3 4 5
25..246–96 7 8 9
35..3310–1210 11 12
45..4213–1413 14
55..511515

Numbers per row = rows - i + 1 — total prints = 1 + 2 + ... + n = n(n+1)/2.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: reset k = 1 inside the outer loop and watch the sequence restart each row.

2. Pattern Series Base

Foundation for continuous-counter patterns with shrinking row width.

Example: compare with Program 35 (right-aligned) and Program 39 next.

3. Console Formatting Drills

Practice {0,3} fixed-width columns when numbers become two digits.

Example: change to {0,4} for wider columns on large row counts.

4. Shrinking inner bound

Inner loop stops at i instead of 1 — one fewer number each row.

Example: row i prints rows - i + 1 numbers from counter k.

5. Complexity Intuition

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.

6. Input Validation Labs

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.

Advantages

Why this pattern earns a permanent spot in beginner C# courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    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.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Counter Before Loops

    Declare k = 1 once before the outer loop — never reset inside unless you want per-row numbering.

  2. 2. Prefer TryParse

    Avoid crashes when the user types letters instead of a number.

  3. 3. Keep WriteLine Outside

    Only call WriteLine() after the inner loop finishes the row.

  4. 4. Trace inner bound on Paper

    Write j = rows..i and count rows - i + 1 numbers per row before coding.

  5. 5. Dry-Run rows = 3

    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.

Common Pitfalls

Mistakes that commonly break decreasing-width continuous number triangles.

  1. 1. WriteLine Inside the Inner Loop

    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.

  2. 2. Resetting k Each Row

    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.

  3. 3. Wrong Inner Bound

    Using j >= 1 prints the same width every row — no shrinking effect.

    → Keep for (j = rows; j >= i; j--) — stop at i, not 1.

  4. 4. Missing {0,3} Format

    Without fixed-width formatting, two-digit numbers misalign columns.

    → Use Console.Write("{0,3}", k++) for aligned output.

  5. 5. Blind Convert.ToInt32

    Letters or empty input throw FormatException.

    → Prefer int.TryParse and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single row

Output is just 1 — one number from counter k.

rows = 0

Empty pattern

Outer loop never runs when rows < 1 — print nothing or show a message.

Negative

rows < 1

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 1 2 and 3.

Bad input

Non-numeric ReadLine

Convert.ToInt32 throws — use TryParse.

Large rows

Large row count

Total prints = n(n+1)/2 — grows quadratically with rows.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Palindrome triangle

  • Review Program 37
  • Palindrome rows with dual inner loops

2. Next in series

  • Continue with Program 39
  • Next pattern in the number-pattern series

3. Counter trace

  • Prove on paper: row i prints rows - i + 1 numbers
  • Inner loop = j = rows..i, counter k never resets

4. Safe input loop

  • Use TryParse until rows >= 1
  • Then draw the triangle

Notes

  • Counter rule. Declare k = 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.
  • Validate rows >= 1 for interactive programs; rows = 1 prints a single 1.
  • Total prints = 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().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Use for (i = 1; i <= rows; i++) in the outer loop
  • Inner: for (j = rows; j >= i; j--) Console.Write("{0,3}", k++);
  • Declare int k = 1; before both loops
  • Prefer int.TryParse over bare Convert.ToInt32
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call WriteLine inside the inner loop
  • Reset k = 1 inside the outer loop
  • Use j >= 1 in the inner loop (no shrinking width)
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this decreasing-width triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

rows..i

Shrinking width

Code
0 03

{0,3}

Fixed-width

Code
04

Row break

WriteLine after j

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Continuous numbers starting from 1, but each row has one fewer number: 5 on row 1, then 4, 3, 2, and 1 — totaling 15 numbers for 5 rows.
Counter k starts at 1 before the loops and increments with k++ each time a number prints — it is never reset inside the outer loop.
Row i prints rows - i + 1 numbers — the inner loop runs from j = rows down to i.
The format specifier reserves 3 columns per number, keeping columns aligned when values become two digits.
Program 35 is right-aligned with leading spaces. Program 38 is left-aligned with decreasing row width and the same continuous counter.
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 number 1.

Did you Know? 🔊

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

Continue to Program 39

Move on to the next pattern in the C# number-pattern series.

Program 39 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.

12 people found this page helpful