An inverted repeating number triangle prints digit i exactly i times, counting i down from rows so the first row is widest.
Remember
Rule: for i = n..1, print i exactly i times
55555
4444
333
22
1 ← 5 rows
In C you solve it with two nested for loops: the outer loop picks the digit, the inner loop repeats it with printf("%d", i), then printf("\n") ends the row. Program 10 is the growing twin (5, 44, 333…).
Approach
How to Solve It
Outer loop counts down from rows to 1. Inner loop repeats digit i exactly i times. End each row with printf("\n").
Method
Idea
Best for
Nested loops
Outer picks digit n..1; inner repeats it i times
Learning, interviews, exams
putchar
Write each repeated digit without a format string
Leaner demos once loops click
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print i (no newline)
print newline
Cheat sheet
Goal
Pattern
Pick digit (count down)
for (i = rows; i >= 1; i--)
Repeat digit i exactly i times
for (j = 1; j <= i; j++) printf("%d", i);
End the row
printf("\n");
putchar form
putchar('0' + i); inside the same inner loop
Growing twin (Program 10)
for (j = rows; j >= i; j--) printf("%d", i);
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", i)
Stays on the same line
Each repeat of digit i
printf("\n")
Ends the current line
After the inner loop
Print all repeats without a newline, then end the row once.
Try it
Live Preview
Change the row count and the inverted triangle updates instantly — capped at 9 so every digit stays a single character.
Whole numbers from 1 to 9 (single digits). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
55555
4444
333
22
1
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, how many times the inner loop runs, and the printed row.
i
Inner j
Repeats
Printed row
4
1..4
4
4444
3
1..3
3
333
2
1..2
2
22
1
1..1
1
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 putchar shortcut. Use View Output for sample results.
Example 1 — Fixed rows = 5
Outer loop picks the digit; inner loop repeats it exactly i times.
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
printf("%d", i);
printf("\n");
}
return 0;
}
Output
55555
4444
333
22
1
How It Works
1. Count down.for (i = rows; i >= 1; i--) yields 5, 4, 3, 2, 1 — the digit for each row.
2. Match repeats to digit. When i = 5, the inner loop runs five times (55555); when i = 1, just once (1).
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 integer.\n");
return 1;
}
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
printf("%d", i);
printf("\n");
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
4444
333
22
1
How It Works
1. Validate rows. Require scanf success and rows >= 1.
2. Same nested loops. Four rows start at 4444 and end at 1.
3. Safer demos. Cap at single-digit rows if you want a tight look:
Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
printf("Enter a whole number from 1 to 9.\n");
return 1;
}
Example 3 — putchar Shortcut
Same loops with putchar('0' + i) — best for single-digit rows (1–9).
C
#include <stdio.h>
int main(void)
{
int rows = 5;
int i, j;
for (i = rows; i >= 1; i--)
{
for (j = 1; j <= i; j++)
putchar('0' + i);
putchar('\n');
}
return 0;
}
Output
55555
4444
333
22
1
How It Works
1. Digit as char.'0' + i maps 1..9 to characters '1'..'9'.
2. Same shape. Identical output to Example 1 — keep the printf version for exams that expect format strings.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong twin
for (j = rows; j >= i; j--)
That is Program 10 (5, 44, 333…). Keep j from 1 to i so repeats equal the digit.
\n inside
printf("\n") inside the inner loop
That puts each digit on its own line. Print repeats without a newline; call printf("\n") only after the row finishes.
off-by-one
j < i instead of j <= i
You drop one repeat on every row (e.g. 55555 becomes 5555).
rows = 1
Single digit
Output is just 1 — a good sanity check.
rows ≤ 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
multi-digit
rows > 9
printf("%d", 10) prints two characters. Labs usually stick to 1–9; putchar('0' + i) also assumes a single digit.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
putchar shortcut (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 = n..1, print digit i exactly i times.
Inner bound:j from 1 to i matches repeats to the digit and shrinks each row.
printf vs newline: repeats stay on the line; printf("\n") advances after each row.
Next step: Program 12 shrinks a different repeating sequence.
One line: for each digit from n down to 1, print it i times with printf, then printf("\n").
Frequently Asked Questions
The outer loop decreases i from rows down to 1, and the inner loop runs j from 1 to i. Row i repeats digit i exactly i times, so the widest row is first and each next row is shorter.
for (j = 1; j <= i; j++) printf("%d", i) runs the inner body i times. When i is 5 you get 55555; when i is 1 you get a single 1.
printf("%d", i) stays on the same line. printf("\n") ends the current line. Repeated digits use printf; the row break uses printf("\n") after the inner loop.
Program 10 grows repeat count as the digit shrinks (5, 44, 333, 2222, 11111) using for (j = rows; j >= i; j--). Program 11 repeats digit i exactly i times (55555, 4444, 333, 22, 1) using for (j = 1; j <= i; j++).
O(n²) where n is the number of rows. Total digit prints equal n+(n-1)+…+1 = n(n+1)/2.
Yes. putchar('0' + i) writes one digit when i is 1..9. Nested loops with printf are better for learning; putchar is a leaner shortcut once the bounds make sense.
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?
Row digit i repeats exactly i times. The outer loop counts down from rows, so the first row is widest (rows copies of rows) and each line shortens — still O(n²) total prints.