Shape Rule
1..i digits on row i
Row 1 prints 1, row 2 prints 12, growing until row rows prints 1..rows.

The ascending number triangle pattern grows one digit per row: nested loops, cout vs cout << "\n", and a clear visual result. This tutorial covers the shape rule, loop structure, a live preview, algorithm steps, worked C++ examples, edge cases, and complexity.
1..i digits on row i
Row 1 prints 1, row 2 prints 12, growing until row rows prints 1..rows.
Rows
for (i = 1; i <= rows; i++) walks each line from one digit up to the full width.
Digits
for (j = 1; j <= i; j++) prints digits 1 through i on that row.
Same line / next line
Digits use cout << j; end each row with cout << "\n".
1–20 rows
Pick a row count and draw the ascending number triangle instantly in the browser.
Complexity
Total digit prints = n(n+1)/2; extra memory stays O(1).
An ascending number triangle pattern starts with one digit on row 1 and grows by one digit each row. Each row prints consecutive digits from 1 up to i, expanding from top to bottom.
In C++ you usually solve it with two nested for loops: the outer loop picks the row, the inner loop prints digits 1..i on that row, then cout << "\n" moves to the next line.
It is a natural follow-up after Program 4’s left-aligned descending triangle. Once nested loops and cout/cout << "\n" click, pyramids, diamonds, and hollow shapes become much easier.
On row i, print digits 1 through i.
Outer counts up rows; inner prints digits 1..i.
cout << j in the inner loop; cout << "\n" after.
Natural step after Program 4; gateway to pyramid and hollow patterns.
In short: for each row i from 1 up to rows, print digits 1..i with cout << j, then call cout << "\n".
Given a positive integer rows, print an ascending number triangle: each row i shows digits 1 through i, with the outer loop counting from 1 up to rows.
// rows = 5
//1
//12
//123
//1234
//12345 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines to print (typically ≥ 1). |
| Printed output | text | Each row prints 1..i; the first row has one digit, the last row has rows digits. |
for i from 1 to rows:
for j from 1 to i:
print j (no newline)
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | Outer rows + inner digits | Learning and interviews |
| Spaced output | cout << j << " " | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk each row | for (i = 1; i <= rows; i++) |
Print digits 1..i | for (j = 1; j <= i; j++) cout << j; |
| End the row | cout << "\n"; |
| Spaced digits | cout << j << " "; |
| Program 1 contrast | for (i = rows; i >= 1; i--) (descending outer) |
Same ascending number triangle — different ways to control rows and formatting.
i = 1..rowsCounts up each row — triangle grows
j = 1..iPrints ascending digits per row
cout << j << " "Optional space between numbers on each row
cin >> rowsValidate row count when reading cin input
Reach for this triangle when teaching or testing nested-loop basics.
Natural follow-up after Program 4 — same inner loop but the outer loop counts up instead of shrinking rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with cin for a flexible row count.
Compare Program 1 (descending outer) and Program 6 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a row count between 3 and 9 and draw the ascending number triangle in the browser.
Three complete C++ programs — fixed rows, user input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows of the ascending number triangle with nested loops.
rows = 5Hard-coded height — ideal for first demos and screenshots.
#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++)
cout << j;
cout << "\n";
}
return 0;
} When i = 1, the inner loop prints 1. When i = 5, it prints 12345 — each row adds one more digit. cout << "\n" after the inner loop starts the next row.
Let the user choose the height at runtime.
Read rows with cin >> rows (check cin.fail()) and validate the result.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
cout << "Enter the number of rows: ";
cin >> rows;
if (cin.fail() || rows < 1)
return 1;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
cout << j;
cout << "\n";
}
return 0;
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Add a space between digits for easier reading on each row.
Keep rows = 5 but print each digit followed by a space.
#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++)
cout << j << " ";
cout << "\n";
}
return 0;
} Only the print statement changes — cout << j << " " instead of cout << j. Loop bounds stay the same as Example 1.
#include <iostream> using namespace std; brings in cout and cin. Set rows (fixed or from input).
for (i = 1; i <= rows; i++) selects the current line, starting at one digit and growing.
for (j = 1; j <= i; j++) prints digits 1..i with cout << j.
cout << "\n" ends the row so the next outer iteration starts fresh.
Total digit prints: 1+2+…+n = n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5Trace each outer-loop value of i (counting up) and count how many digits the inner loop prints.
i | Inner j range | Printed row | Digits this row |
|---|---|---|---|
1 | 1..1 | 1 | 1 |
2 | 1..2 | 12 | 2 |
3 | 1..3 | 123 | 3 |
4 | 1..4 | 1234 | 4 |
5 | 1..5 | 12345 | 5 |
Total digit prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change j <= i and watch the shape change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: Program 4 shrinks each row from rows down to i.
Practice cout << j vs cout << "\n" without complex math.
Example: put cout << "\n" inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: use cout << j << " " for spaced digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for n = 10 → 55.
Pair the pattern with cin.fail() checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C++ courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row grows by one digit.
Small habits that keep number-pattern code clean.
Use rows (or n) and keep i/j for row/column — or rename to row/col.
cin.fail()Avoid crashes when the user types letters instead of a number.
Only call cout << "\n" after the inner loop finishes the row.
1..rows with j <= i matches “row i prints digits 1..i” naturally.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits, you almost certainly put cout << "\n" inside the inner loop.
Mistakes that commonly break ascending number triangles.
Each digit lands on its own line — you get a column, not a triangle.
→ Use cout << j for digits; cout << "\n" only after the inner loop.
j <= rows prints a rectangle; wrong outer bounds flatten or invert the shape.
→ For this shape, keep j <= i.
Omitting cout << "\n" glues every digit onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input leave rows unread when cin.fail() is not checked.
→ Check cin.fail() and re-prompt on failure.
Switching to i = 0 without adjusting the inner bound prints an empty first row or wrong counts.
→ If 0-based, print digits 1..i+1 (e.g. j <= i + 1).
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n²/2 characters — fine for labs, noisy for huge n.
Unchecked cin leaves rows unset — check cin.fail().
Try cout << j << " " for spaces between numbers.
Try these variations to lock in the pattern.
cout << j << " " between digitsn(n+1)/2 — hence O(n²) time.cout stays on the line; cout << "\n" advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.Quick Takeaway: outer loop picks the row, inner loop prints digits 1..i, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Spaced output (Example 3) | O(rows²) | O(1) |
The ascending number triangle pattern is a small nested-loop exercise with lasting payoff: row/column thinking, cout vs cout << "\n", and O(n²) intuition. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 6 for the next pattern in the series.
Row i prints 1..i — keep cout << j for digits and cout << "\n" for the break, and validate row counts when reading input.
for (i = 1; i <= rows; i++) in the outer loopcout << j for digits and cout << "\n" after each rowrows ≥ 1 for interactive programscin.fail() with return-value checks over unchecked readscout << "\n" inside the inner digit looprows = 1 edge casePrint the triangle the beginner-friendly way.
Row i prints 1..i
DefinitionControls each row
CodePrints digits with cout << j
cout << "\n" ends each row
O(n²) time
AnalysisRow i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — still O(n²) total prints.
Move on to the next pattern in the C++ number-pattern series.
11 people found this page helpful