Number Pattern 1, 2 6, 3 7 10 in C++

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

What You’ll Learn

How to generate this triangular number pattern in C++:

1, 2 6, 3 7 10, 4 8 11 13, 5 9 12 14 15

We start each row with the row number and compute the next values by adding a decreasing offset.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
1

Complete C++ Program

For each row, we print i first, then compute subsequent values using a helper offset that decreases as we move across the row.

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

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

    for (i = 1; i <= rows; i++) {
        cout << i;

        int m = rows - 1;
        int k = i + m;

        for (j = 1; j < i; j++) {
            cout << " " << k;
            m--;
            k = k + m;
        }

        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Print the row starter

Each row begins with i (so row 4 starts with 4).

Row start
2

Initialize the offsets

m = rows - 1 is the first jump size, and k = i + m computes the next value to print.

Setup
3

Print k, then shrink the jump

After printing k, we do m-- and update k = k + m to get the next value in the row.

Row filling
4

Move to the next line

cout << "\\n"; ends the current row.

Line break
=

Jump-based triangle

The offsets shrink from rows-1 down to 1 as you move across a row, which creates the non-consecutive sequence.

2

Variation — User Input Version

Let the user choose how many rows to print.

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

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

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

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

    for (i = 1; i <= rows; i++) {
        cout << i;

        int m = rows - 1;
        int k = i + m;

        for (j = 1; j < i; j++) {
            cout << " " << k;
            m--;
            k = k + m;
        }

        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print aligned columns using a fixed width (e.g., setw(3))
  • Increase rows to explore larger patterns
  • Store each row in a vector first, then print (useful for formatting)
  • Replace numbers with characters to build related alphabet patterns
  • Add input validation with cin.fail() and clear the stream

Avoid

  • Hardcoding m = 4 if you want the pattern to scale with rows
  • Printing trailing spaces (print the leading starter without a preceding space)
  • Using endl in tight loops (unnecessary flushing)
  • Not checking for invalid input when using cin

Key Takeaways

1

Each row starts by printing its row number i.

2

The next values are computed using a decreasing offset variable m.

3

The pattern prints \(n(n+1)/2\) numbers for \(n\) rows (so it’s \(O(n^2)\)).

4

Resetting m per row keeps the same jump structure across rows.

❓ Frequently Asked Questions

Start each row with i. Then set m = rows - 1 and k = i + m. Print k, decrement m, and update k = k + m until the row is complete.
Because the offset m starts large and shrinks across the row. Those offsets are added to k, creating non-consecutive jumps.
Yes. Increase rows (or use the user-input version). The same logic works for any positive number of rows.
O(n²) for \(n\) rows, because the program prints \(n(n+1)/2\) numbers.

Explore More C++ Number Patterns!

Try modifying the row count and formatting to better understand how offsets shape number patterns.

All Number Patterns →
Did you know?

The total number of values printed in any triangle of n rows 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