Sequential Number Triangle (Starts at 11) in C++

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

What You’ll Learn

How to print a sequential number triangle in C++ where the first value is 11 and each row continues the sequence.

We’ll use nested for loops and a small formula 9 + i + j to generate each printed value.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19
1

Complete C++ Program

Fixed five rows: the outer loop controls the row count, and the inner loop prints i values using 9 + i + j.

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

int main() {
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            if (j > 1) cout << ' ';
            cout << (9 + i + j);
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Set the number of rows

int rows = 5; defines the triangle height.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) prints one line per iteration, and row i contains i values.

Row control
3

Inner loop (columns)

for (j = 1; j <= i; j++) controls how many numbers print on the current row.

Column control
4

Compute and print the value

The expression 9 + i + j generates the sequence starting at 11 (i=1, j=1) and increases as j and i grow.

Value rule
=

Sequential number triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows at runtime using cin:

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

int main() {
    int rows;
    int i, j;

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

    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            if (j > 1) cout << ' ';
            cout << (9 + i + j);
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate input (e.g., check cin.fail()) before using rows
  • Start from a different base by changing 9 (e.g., use base and print base + i + j)
  • Use fixed-width formatting for neat alignment (e.g., setw(3))
  • Print the same triangle without spaces for a compact look
  • Try the descending-width version by using for (j = i; j <= rows; j++)

Avoid

  • Printing an extra trailing space if you care about exact formatting (use a separator rule)
  • Hard-coding 5 in multiple places (use rows)
  • Forgetting the newline after each row
  • Using endl inside loops (it flushes output and slows printing)

Key Takeaways

1

The outer loop chooses the row (i = 1..rows).

2

The inner loop prints i values on row i (j = 1..i).

3

The value formula 9 + i + j makes the first number 11 and keeps the sequence increasing.

4

Work done is triangular: n(n+1)/2, so overall time is O(n²).

❓ Frequently Asked Questions

On the first row, i = 1 and j = 1, so 9 + i + j = 11.
Replace 9 with a variable base and print base + i + j. For example, if base = 19, the first value becomes 21.
Because the inner loop runs to j <= i. When i increases, the loop prints one additional value on that row.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C++ Number Patterns!

Practice nested loops by generating sequences, triangles, and more interesting console patterns.

All Number Patterns →
Did you know?

The expression 9 + i + j is a simple example of mapping a 2D grid position (row, column) into a value. Changing the formula is an easy way to create entirely new number patterns without changing the loop structure.

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