A left-shifted descending number triangle starts each row at i and prints through rows, so the leftmost digit drops off one by one.
Remember
Rule: for i from 1 to rows
print j from i to rows
12345
2345
345
45
5 ← rows = 5
Follows the descending 1..i triangle in Program 1; next is the reverse descending triangle in Program 3.
Approach
How to Solve It
Outer loop grows i from 1 to rows. Inner loop starts at i (not 1) and prints through rows.
Method
Idea
Best for
Start at i
for (j = i; j <= rows; j++) cout << j;
Learning, interviews, exams
String row
Append digits to a string, then one cout per row
Same shape, one print per line
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i to rows:
print j
print newline
Cheat sheet
Goal
Pattern
Grow start digit
for (i = 1; i <= rows; i++)
Print i..rows
for (j = i; j <= rows; j++) cout << j;
End of row
cout << "\n";
Add spaces
cout << j << " ";
Program 1 shape
for (i = rows; i >= 1; i--) with j = 1..i
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << j
Stays on the same line
Each digit on the row
cout << "\n"
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the left-shifted triangle updates instantly — capped at 9 for readable demos.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
12345
2345
345
45
5
Trace
Worked Walkthrough — rows = 5
Trace how raising the inner start i drops the leftmost digit each row.
i
Inner range
Prints
1
j = 1..5
12345
2
j = 2..5
2345
3
j = 3..5
345
4
j = 4..5
45
5
j = 5..5
5
Total digits = n + (n-1) + … + 1 = n(n+1)/2 → O(n²).
Code
C++ Programs
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 — inner loop starts at i and prints through rows.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output
12345
2345
345
45
5
How It Works
1. Outer picks the start.i is both the row index and the first digit of that row.
2. Inner prints i..rows.for (j = i; j <= rows; j++) cout << j; — not j = 1.
3. Newline once. Call cout << "\n" only after the inner loop finishes.
Example 2 — User Input Rows
Read rows with cin, validate, then use the same i..rows core.
C++
#include <iostream>
using namespace std;
int main()
{
int rows;
int i, j;
cout << "Enter the number of rows: ";
if (!(cin >> rows) || rows <= 0)
{
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
1234
234
34
4
How It Works
1. Prompt and validate. Reject failed reads and non-positive values before printing.
2. Same core. Start-at-i matches 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 > 9)
{
cout << "Enter a whole number from 1 to 9.\n";
return 1;
}
Example 3 — Compact rows = 3
Same structure with only three rows — easy to confirm the shifting start.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
cout << j;
cout << "\n";
}
return 0;
}
Output
123
23
3
How It Works
1. Three rows.i = 1 → 123; i = 2 → 23; i = 3 → 3.
2. Trace on paper. If you start j at 1 instead of i, you get Program 1’s ascending triangle — not this shift.
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.
j = 1
Wrong start
Starting at j = 1 prints 1..rows every row. For this shape keep j = i.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.
rows = 1
Single digit
Output is just 1 — a good sanity check for input validation.
rows > 9
Multi-digit glue
Digits run together (…91011…). Cap demos or add spaces with cout << j << " ".
cout << i
Repeating rows
Printing i instead of j fills each row with the same digit. Print the loop variable j.
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)
Total digits printed = n + (n-1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Start at i: the inner loop begins at j = i, not j = 1.
Print through rows: each row is digits i..rows, so the left edge shifts.
Break the row:cout << j in the inner loop; cout << "\n" after it.
Complexity:O(n²) time from n(n+1)/2 digits; O(1) extra space.
One line: for each i from 1 to rows, print j from i to rows, then newline.
Frequently Asked Questions
A left-shifted descending triangle: for rows=5 you get 12345 / 2345 / 345 / 45 / 5 — each row starts at i and prints through rows.
The inner loop starts at j = i, not j = 1. When i increases, each row begins at that value and prints until rows.
Because each next row starts at a higher i, numbers below i are not printed — the triangle shifts left visually.
Program 1 counts the outer loop down and prints 1..i. Program 2 counts up and prints i..rows — same total prints, different shape.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Yes. Use cout << j << " " instead of cout << j if you want spaced output.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are n+(n-1)+…+1 = n(n+1)/2.
🤔
Did you know?
Each row starts at i and prints through rows, so the left edge shifts right each line — still O(n²) total prints for n rows.