Continuous Number Triangle in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a triangle of consecutive integers in C++:

1, 2 3, 4 5 6, 7 8 9 10

We use a counter variable that increments after each print, so numbers continue across rows.

⭐ Pattern Output

For rows = 4, the pattern looks like this:

Output
1
2 3
4 5 6
7 8 9 10
1

Complete C++ Program

The outer loop controls rows. The inner loop prints values using k++, which increments after each print.

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

Initialize the counter

int k = 1; starts the sequence at 1.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) makes row 1 print 1 number, row 2 print 2 numbers, and so on.

Row control
3

Inner loop (print and increment)

cout << k++; prints the current value and increments it for the next position.

Number printing
4

New line

cout << "\\n"; moves to the next row after printing each line.

Line break
=

Continuous sequence triangle

Because k is not reset per row, the sequence continues from one line to the next.

2

Variation — User Input Version

Let the user choose the number of rows at runtime.

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    int i, j;
    int k = 1;

    cout << "Enter the number of rows: ";
    cin >> rows;

    if (!cin || rows <= 0) return 0;

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            cout << k++ << " ";
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Remove trailing spaces by printing a space only between values
  • Start from a different number (e.g., k = 10) to shift the sequence
  • Format output with fixed width for aligned columns
  • Print the triangle in reverse by looping i downwards
  • Use the same idea to build multiplication or square tables

Avoid

  • Resetting k inside the loop (you’ll lose the continuous sequence)
  • Using endl in tight loops (unnecessary flushing)
  • Not validating input (negative/zero rows)
  • Mixing row and column variables in the inner loop

Key Takeaways

1

A counter variable (k) carries the sequence across rows.

2

The outer loop controls how many numbers print per row.

3

The total printed numbers are \(n(n+1)/2\) for \(n\) rows.

4

This pattern is a close relative of Floyd’s triangle.

❓ Frequently Asked Questions

Because k is declared outside the outer loop and incremented after each print.
A total of 1+2+…+n = n(n+1)/2 numbers.
It’s the same idea: consecutive numbers printed in a triangular shape. Many tutorials refer to this as Floyd’s triangle.
O(n²) for \(n\) rows, because you print \(n(n+1)/2\) values.

Explore More C++ Number Patterns!

Continuous-number triangles are a classic way to practice nested loops and counters.

All Number Patterns →
Did you know?

The total count of printed numbers in a triangle is a triangular number: \(1+2+\dots+n = n(n+1)/2\).

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