C++ Incremental Number Triangle Pattern (Right-Aligned)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned continuous counter triangle prints a growing sequence with counter k that never resets — leading pads while j > i, then setw(3) << k++.
Remember
Rule: k = 1
for i from 1 to rows
for j from rows down to 1
if j > i: print 3 spaces
else: print k (width 3); k++
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 ← rows = 5
Follows the zero-based triangle in Program 34; next is the right-aligned decreasing triangle in Program 36.
Approach
How to Solve It
Outer loop grows i. One fixed-width inner loop runs rows..1 — print a 3-space pad or the next k with setw(3).
Method
Idea
Best for
Fixed width + k++
Spaces while j > i; else print and increment k
Learning, interviews, exams
setw(3) columns
Keeps two-digit values aligned with single digits
Readable demos past 9
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print 3 spaces
else: print k in width 3; k = k + 1
print newline
Cheat sheet
Goal
Pattern
Grow each row
for (i = 1; i <= rows; i++)
Fixed width
for (j = rows; j >= 1; j--)
Leading pads
if (j > i) cout << " ";
Next number
cout << setw(3) << k++;
End of row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << " " / setw(3) << k++
Stays on the same line
Each pad or number on the row
cout << "\n"
Ends the current line
After the fixed-width loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned counter updates instantly — capped at 7 for readable demos.
Whole numbers from 1 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 numbers
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 5
Trace how leading pads shrink and counter k keeps climbing.
Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — continuous k with setw(3) and leading pads.
C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, k = 1;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << setw(3) << k++;
}
cout << "\n";
}
return 0;
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How It Works
1. Counter lives outside.k starts at 1 and only advances when a number prints.
2. Fixed width. Inner loop always runs 5 times — unused left slots become " ".
3. Width 3 columns.setw(3) keeps 10 and 11 aligned with single digits.
Example 2 — User Input Rows
Read rows with cin, validate, then use the same counter and pad logic.
C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int rows;
int i, j, k = 1;
cout << "Enter rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << setw(3) << k++;
}
cout << "\n";
}
return 0;
}
Output (when user enters 3)
Enter rows: 3
1
2 3
4 5 6
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Pads + setw(3) << k++ match Example 1 — only rows comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> rows) || rows < 1 || rows > 7)
{
cout << "Enter a whole number from 1 to 7.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm pads shrink and k never resets.
C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int rows = 3;
int i, j, k = 1;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << setw(3) << k++;
}
cout << "\n";
}
return 0;
}
Output
1
2 3
4 5 6
How It Works
1. Three rows. Ends at 6 — if k reset each row you would see 1, 1 2, 1 2 3.
2. Trace on paper. If the inner loop stops at i instead of rows, right alignment is lost.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = 1
Resetting the counter
Putting k = 1 inside the outer loop restarts every row. Declare k once before both loops.
no setw
Broken columns
Without setw(3), two-digit values misalign with single digits. Keep fixed width.
width ≠ 3
Pad mismatch
Leading pads must match number width — use " " (3 spaces) with setw(3).
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, you get one cell per line. Call it only after the loop.
rows = 1
Single value
Output is just 1 with no leading pads. A good sanity check for input validation.
cin
Check the stream
Validate cin >> rows before looping — a failed read leaves rows unset or stale.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact rows = 3 (Example 3)
O(n²)
O(1)
Each of n rows visits n columns (pads or numbers). Numbers printed = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Continuous k: declare once outside; use k++ only when printing a number.
Fixed width:j = rows..1 with pads when j > i keeps the triangle right-aligned.
setw(3): match pad width to number width so two-digit values stay aligned.
Complexity:O(n²) time; O(1) extra space.
One line: for each row i, walk j = rows..1 and print a pad or setw(3) << k++, then newline.
Frequently Asked Questions
A right-aligned continuous counter triangle: for rows=5 you get indented 1 / 2 3 / 4 5 6 / 7 8 9 10 / 11 12 13 14 15 — k never resets between rows.
Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
setw(3) reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
🤔
Did you know?
A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and setw(3) keeps columns aligned as values grow past single digits.