Java Continuous Number Triangle Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A continuous number triangle prints a growing row count, but the numbers never restart. One counter k keeps rising across every row.

Remember
Rule: set k = 1 once; for row i,
      print k++  i times

1
2 3
4 5 6
7 8 9 10     ← rows = 4

Declare k outside the outer loop. If you reset k each row, you get Program 1’s growing triangle instead. Natural step after Program 19 (fill-with-n) and before jump counters.

How to Solve It

One counter before the loops; print and increment inside the inner loop.

MethodIdeaBest for
Running counterk = 1 once; print k++ i times per rowLearning, interviews, exams
Custom startSame loops; set k to any start valueWhen the sequence should begin at 10, 100, …

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from 1 to i:
        print k, then k = k + 1
    print newline

Cheat sheet

GoalPattern
Start counter onceint k = 1; before the outer loop
Grow row lengthfor (int i = 1; i <= rows; i++)
Print then bumpSystem.out.print(k++ + " ");
End the rowSystem.out.println();
Custom startint k = 10; (or any value)
Total numbersrows * (rows + 1) / 2

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach k++ with a trailing space
System.out.printlnEnds the current lineAfter the inner loop

Print numbers without a newline, then end the row once.

Live Preview

Change the row count and the continuous counter triangle updates instantly.

Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 4 · 10 numbers
1
2 3
4 5 6
7 8 9 10

Worked Walkthrough — rows = 4

Trace k before each row, what gets printed, and k afterward.

ik beforePrintedk after
1112
222 34
344 5 67
477 8 9 1011

Total numbers: 1 + 2 + 3 + 4 = 10 = n(n + 1) / 2 — that is why time is O(n²).

Java Programs

Three complete programs: fixed rows = 4, Scanner input, and a custom start k = 10. Use View Output to reveal sample results.

Example 1 — Fixed rows = 4

Hard-coded height — k starts at 1 and never resets.

Java
public class ContinuousNumberTriangle {
    public static void main(String[] args) {
        int rows = 4;
        int k = 1;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(k++ + " ");
            System.out.println();
        }
    }
}

How It Works

1. Counter once. k = 1 sits before the outer loop so its value survives every row.

2. Row length grows. Row i runs the inner loop i times.

3. Print then bump. k++ prints the current value, then increments for the next print.

After row 1, k is 2. Row 2 therefore prints 2 3, not 1 2.

Example 2 — Rows Input

Read rows at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

public class ContinuousNumberTriangleInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
        int k = 1;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(k++ + " ");
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and read. Ask for a height, then store it with sc.nextInt().

2. Same counter core. k still starts at 1 — only the source of rows changes.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Example 3 — Custom Start k = 10

Shift the whole sequence by starting k at 10 instead of 1.

Java
public class ContinuousNumberCustomStart {
    public static void main(String[] args) {
        int rows = 4;
        int k = 10;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print(k++ + " ");
            System.out.println();
        }
    }
}

How It Works

1. Same loops. Outer and inner bounds match Examples 1 and 2.

2. Different start. Only the initial k changes — k++ still drives the sequence.

3. Same shape. Row lengths are still 1, 2, 3, 4; values just begin at 10.

Edge Cases & Pitfalls

Check these before calling the solution done.

k inside

Reset each row

Declaring k inside the outer loop restarts at 1 every row. Keep k before the loops.

++k vs k++

Post-increment

Use k++ so you print the current value, then bump. Pre-increment ++k skips the starting value.

println inside

Column of numbers

If println is inside the inner loop, each number lands on its own line. Use print for numbers; println only after the inner loop.

rows = 1

Single 1

Output is just 1 — a good sanity check.

rows ≤ 0

Empty output

The outer loop never runs. Validate and require rows ≥ 1.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.

Time and Space Complexity

ProgramTimeExtra space
Running counter (Examples 1–3)O(n²)O(1)

Total prints: 1 + 2 + … + n = n(n + 1) / 2 — still quadratic in n. Only a few scalars are stored.

Key Takeaways

  • Rule: set k = 1 once; row i prints k++ exactly i times.
  • No reset: keep k outside the outer loop so values continue across rows.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: one shared k, print k++ i times per row, then println().

Frequently Asked Questions

Because k is declared once (k = 1) before the loops and incremented with k++ every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so k becomes 2. Row 2 prints k twice: 2, then 3 after k++.
k resets each row and the sequence restarts at 1 — you get a growing triangle (1, 1 2, 1 2 3…), not a continuous counter.
System.out.print(k++ + " ") stays on the same line with a trailing space. System.out.println() ends the current line. Numbers use print; the row break uses println after the inner loop.
Total prints are 1+2+…+n = n(n+1)/2.
Yes. Set k = 10 (or any value) before the loops — the sequence continues from that start. See Example 3.
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt(), require rows ≥ 1, and reject non-numeric input — see Example 2 notes.

Did you know?

A single counter k starts at 1 and increments with k++ on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.

Next: Jump Number Triangle

Move on to the jump number triangle in the Java number-pattern series.

Program 21 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