A continuous number triangle grows one number per row, but a single counter k keeps going across rows — it never restarts at 1.
Remember
Rule: k = 1 before loops
for i from 1 to rows
print i numbers: k, then k++
1
2 3
4 5 6
7 8 9 10 ← rows = 4
Follows the fill-with-5 triangle in Program 19; next is the jump number triangle in Program 21.
Approach
How to Solve It
Declare k = 1 once. Outer loop grows width i; inner loop prints i values with k++ — never reset k per row.
Method
Idea
Best for
Running k++
One counter outside both loops; print and increment each time
Learning, interviews, exams
Custom start
Set k = 10 (or any value) before the loops
Shifted sequences without changing loop logic
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from 1 to i:
print k and a space
k += 1
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Init counter once
int k = 1; before the outer loop
Print and advance
cout << k++ << " ";
End of row
cout << "\n";
Custom start
int k = 10; instead of 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << k++ << " "
Stays on the same line
Each number on the row
cout << "\n"
Ends the current line
After the inner loop finishes
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 — capped at 9 for readable demos.
Whole numbers from 1 to 9. 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 how k carries across rows without resetting.
i
k before
Prints
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 = n(n+1)/2 → O(n²). Keep k outside the outer loop.
Code
C++ Programs
Three complete programs: fixed rows = 4, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 4
Hard-coded height — declare k once, then print with k++.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 4;
int i, j;
int k = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << k++ << " ";
cout << "\n";
}
return 0;
}
Output
1
2 3
4 5 6
7 8 9 10
How It Works
1. Init once.k = 1 sits before both loops so the value carries across rows.
2. Print and advance.cout << k++ << " " prints the current value, then bumps k.
3. Newline once. Call cout << "\n" only after the inner loop finishes.
Example 2 — User Input Rows
Read rows with cin, validate, then use the same running counter.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
int k = 1;
cout << "Enter the number of rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << k++ << " ";
cout << "\n";
}
return 0;
}
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 validate. Reject failed reads and non-positive values before printing.
2. Same core. Running k++ matches Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm that k does not reset.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
int k = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
cout << k++ << " ";
cout << "\n";
}
return 0;
}
Output
1
2 3
4 5 6
How It Works
1. Three rows.i = 1 → 1; i = 2 → 2 3; i = 3 → 4 5 6.
2. Trace on paper. If you set k = 1 inside the outer loop, every row restarts — that is not this pattern.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for four rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset k
Counter restarts each row
Declaring k = 1 inside the outer loop restarts the sequence. Keep k outside both loops.
cout << j
Wrong variable
Printing j instead of k gives 1, 1 2, 1 2 3 — a growing triangle, not a continuous counter.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each number lands on its own line. Call it only after the row finishes.
rows = 1
Single number
Output is just 1 — a good sanity check for input validation.
no space
Digits glued together
Use cout << k++ << " " so multi-digit values stay readable as the counter grows.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Total numbers printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Init once: declare k = 1 before the outer loop so it never resets.
Print and bump:cout << k++ << " " advances the continuous sequence.
Break the row: numbers in the inner loop; cout << "\n" after it.
Complexity:O(n²) time from n(n+1)/2 numbers; O(1) extra space.
One line: set k = 1 once, then for each row i print i values with k++, then newline.
Frequently Asked Questions
A continuous number triangle: for rows=4 you get 1 / 2 3 / 4 5 6 / 7 8 9 10 — a single counter that never resets between rows.
Because k is declared once (k = 1) before the loops and incremented with k++ every print — 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 1..i triangle, not a continuous counter.
cout << k++ << " " stays on the same line with a space. cout << "\n" ends the current line. Numbers use cout; the row break uses cout << "\n" after the inner loop.
Yes. Set k = 10 (or any value) before the loops — the sequence continues from that start.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total prints are 1+2+…+n = n(n+1)/2.
🤔
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.