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.
Approach
How to Solve It
One counter before the loops; print and increment inside the inner loop.
Method
Idea
Best for
Running counter
k = 1 once; print k++i times per row
Learning, interviews, exams
Custom start
Same loops; set k to any start value
When 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
Goal
Pattern
Start counter once
int k = 1; before the outer loop
Grow row length
for (int i = 1; i <= rows; i++)
Print then bump
System.out.print(k++ + " ");
End the row
System.out.println();
Custom start
int k = 10; (or any value)
Total numbers
rows * (rows + 1) / 2
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each k++ with a trailing space
System.out.println
Ends the current line
After the inner loop
Print numbers without a newline, then end the row once.
Try it
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 resultrows = 4 · 10 numbers
1
2 3
4 5 6
7 8 9 10
Trace
Worked Walkthrough — rows = 4
Trace k before each row, what gets printed, and k afterward.
i
k before
Printed
k after
1
1
1
2
2
2
2 3
4
3
4
4 5 6
7
4
7
7 8 9 10
11
Total numbers: 1 + 2 + 3 + 4 = 10 = n(n + 1) / 2 — that is why time is O(n²).
Code
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();
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
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();
}
}
Output (when user enters 4)
Enter the number of rows: 4
1
2 3
4 5 6
7 8 9 10
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();
}
}
}
Output
10
11 12
13 14 15
16 17 18 19
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.