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

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:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15Complete 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.
#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
Print the row starter
Each row begins with i (so row 4 starts with 4).
Initialize the offsets
m = rows - 1 is the first jump size, and k = i + m computes the next value to print.
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.
Move to the next line
cout << "\\n"; ends the current row.
Jump-based triangle
The offsets shrink from rows-1 down to 1 as you move across a row, which creates the non-consecutive sequence.
Variation — User Input Version
Let the user choose how many rows to print.
#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
rowsto 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 = 4if you want the pattern to scale with rows - Printing trailing spaces (print the leading starter without a preceding space)
- Using
endlin tight loops (unnecessary flushing) - Not checking for invalid input when using
cin
Key Takeaways
Each row starts by printing its row number i.
The next values are computed using a decreasing offset variable m.
The pattern prints \(n(n+1)/2\) numbers for \(n\) rows (so it’s \(O(n^2)\)).
Resetting m per row keeps the same jump structure across rows.
❓ Frequently Asked Questions
i. Then set m = rows - 1 and k = i + m. Print k, decrement m, and update k = k + m until the row is complete.m starts large and shrinks across the row. Those offsets are added to k, creating non-consecutive jumps.rows (or use the user-input version). The same logic works for any positive number of rows.Explore More C++ Number Patterns!
Try modifying the row count and formatting to better understand how offsets shape number patterns.
The total number of values printed in any triangle of n rows is a triangular number: \(1+2+\dots+n = n(n+1)/2\).
12 people found this page helpful
