C++ Continuous Number Triangle Pattern

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

What Is This Pattern?

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.

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.

MethodIdeaBest for
Running k++One counter outside both loops; print and increment each timeLearning, interviews, exams
Custom startSet k = 10 (or any value) before the loopsShifted 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

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Init counter onceint k = 1; before the outer loop
Print and advancecout << k++ << " ";
End of rowcout << "\n";
Custom startint k = 10; instead of 1

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << k++ << " "Stays on the same lineEach number on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

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

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 result rows = 4 · 10 numbers
1
2 3
4 5 6
7 8 9 10

Worked Walkthrough — rows = 4

Trace how k carries across rows without resetting.

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

Total numbers = n(n+1)/2 → O(n²). Keep k outside the outer loop.

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;
}

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;
}

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;
}

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.

Time and Space Complexity

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

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.

Next: Jump Number Triangle

Continue with a counter that jumps by an increasing step each row.

Program 21 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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