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

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:
11
12 13
13 14 15
14 15 16 17
15 16 17 18 19Complete C++ Program
Fixed five rows: the outer loop controls the row count, and the inner loop prints i values using 9 + i + j.
#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
Set the number of rows
int rows = 5; defines the triangle height.
Outer loop (rows)
for (i = 1; i <= rows; i++) prints one line per iteration, and row i contains i values.
Inner loop (columns)
for (j = 1; j <= i; j++) controls how many numbers print on the current row.
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.
Sequential number triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows at runtime using cin:
#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 usingrows - Start from a different base by changing
9(e.g., usebaseand printbase + 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
5in multiple places (userows) - Forgetting the newline after each row
- Using
endlinside loops (it flushes output and slows printing)
Key Takeaways
The outer loop chooses the row (i = 1..rows).
The inner loop prints i values on row i (j = 1..i).
The value formula 9 + i + j makes the first number 11 and keeps the sequence increasing.
Work done is triangular: n(n+1)/2, so overall time is O(n²).
❓ Frequently Asked Questions
i = 1 and j = 1, so 9 + i + j = 11.9 with a variable base and print base + i + j. For example, if base = 19, the first value becomes 21.j <= i. When i increases, the loop prints one additional value on that row.Explore More C++ Number Patterns!
Practice nested loops by generating sequences, triangles, and more interesting console patterns.
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.
12 people found this page helpful
