A left-shifted descending number triangle prints digits i..rows on each row, so every line starts one digit higher and the left edge appears to slide right.
Remember
Rule: for i = 1..n, print i..n
12345
2345
345
45
5 ← 5 rows
In C the outer loop picks the row start i, the inner loop prints j from i to rows, then printf("\n") moves to the next line. Compare with Program 1, which prints 1..i while counting the outer loop down.
Approach
How to Solve It
Master the nested loops first, then optionally space the digits for easier reading.
Method
Idea
Best for
Nested loops
Outer = start 1..n, inner = digits i..n via printf
Learning, interviews, exams
Spaced digits
Same loops with printf("%d ", j)
Clearer tracing by hand
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i to rows:
print j (no newline)
print newline
Cheat sheet
Goal
Pattern
Pick row start
for (i = 1; i <= rows; i++)
Print digits i..rows
for (j = i; j <= rows; j++) printf("%d", j);
End the row
printf("\n");
Spaced form
printf("%d ", j); instead of printf("%d", j)
Program 1 shape instead
for (i = rows; i >= 1; i--) with j = 1..i
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j)
Stays on the same line
Each digit j
printf("\n")
Ends the current line
After the inner loop
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the left-shifted triangle updates instantly — including the total digit count.
Whole numbers from 1 to 9 (keeps digits single-width). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
12345
2345
345
45
5
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and the digits the inner loop prints.
i
Inner j
Printed row
Digits
1
1..4
1234
4
2
2..4
234
3
3
3..4
34
2
4
4..4
4
1
Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.
Code
C Programs
Three complete programs: fixed height, scanf input, and a spaced-digit variant. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop picks the start digit; inner loop prints from i through rows.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Output
12345
2345
345
45
5
How It Works
1. Pick the start.for (i = 1; i <= rows; i++) yields 1, 2, 3, 4, 5 as row starts.
2. Print digits. When i = 1, the inner loop prints 12345; when i = 5, just 5.
3. Newline.printf("\n") after the inner loop starts the next shorter row.
Example 2 — User Input
Read the row count with scanf and reject non-positive values.
C
#include <stdio.h>
int main(void)
{
int rows;
int i, j;
printf("Enter the number of rows: ");
if (scanf("%d", &rows) != 1 || rows < 1)
{
printf("Please enter a positive whole number.\n");
return 1;
}
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Output (when user enters 4)
1234
234
34
4
How It Works
1. Validate rows. Check scanf; require rows >= 1.
2. Same nested loops. Four rows start at 1234 and end at 4.
Example 3 — Spaced Digits
Same loops with printf("%d ", j) so digits are easier to read.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = i; j <= rows; j++)
printf("%d ", j);
printf("\n");
}
return 0;
}
Output
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
How It Works
1. Same structure. Only the format string changes — loops stay identical to Example 1.
2. Space after each digit.printf("%d ", j) separates values for clearer scanning.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong start
j = 1 instead of j = i
That prints 1..rows every row (a rectangle). Keep for (j = i; j <= rows; j++).
newline early
printf("%d\n", j) inside the inner loop
That puts each digit on its own line. Use printf("%d", j); call printf("\n") only after the row finishes.
off-by-one
j < rows instead of j <= rows
You drop the last digit on every row (e.g. 12345 becomes 1234).
rows = 1
Single 1
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Unchecked scanf
Always check scanf’s return value so non-numeric input does not leave rows uninitialized.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
Spaced digits (Example 3)
O(n²)
O(1)
There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: for i = 1..n, print digits i..n with printf.
Start at i: the inner loop begins at j = i, which is what creates the left shift.
printf vs newline: digits stay on the line; printf("\n") advances after each row.
Next step: Program 3 reverses the digit order on each row.
One line: for each start from 1 to n, print i..n with printf, then printf("\n").
Frequently Asked Questions
The inner loop starts at j = i, not j = 1. When i increases (1, 2, 3, …), 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 anymore — the triangle shifts left visually.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Program 1 counts the outer loop down and prints 1..i. Program 2 counts up and prints i..rows — same total prints, different shape.
Change the rows variable or read it with scanf. The outer loop runs from 1 to rows, and the inner loop prints j from i to rows.
Yes. Use printf("%d ", j) instead of printf("%d", j) — see Example 3.
O(n²) for n rows because total prints are n+(n-1)+…+1 = n(n+1)/2.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
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.