An inverted repeating number triangle starts widest and shortens each line — digit i repeats exactly i times, counting down from rows.
Remember
Rule: outer i = rows..1; inner j = 1..i; print i
55555
4444
333
22
1 ← rows = 5
Same digit/count rule as Program 9, but the outer loop counts down so the peak digit appears first. Different from Program 10, where repeats grow while the digit falls.
Approach
How to Solve It
Outer loop from rows down to 1. Inner loop from 1 to current i, printing i each time. End the line after the inner loop.
Method
Idea
Best for
Nested loops
Outer down, inner 1..i, print i
Learning, interviews, exams
String.repeat
One println per row
When the bounds already make sense
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print i
print newline
Cheat sheet
Goal
Pattern
Outer (shrink width)
for (int i = rows; i >= 1; i--)
Inner (repeat count)
for (int j = 1; j <= i; j++) System.out.print(i);
End the row
System.out.println();
Repeats on row i
i (same as the digit)
Shortcut
String.valueOf(i).repeat(i)
Vs Program 9
Same rule; outer counts down instead of up
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit i
System.out.println
Ends the current line
After the inner loop
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the inverted repeating triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
55555
4444
333
22
1
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch digit and repeat count both equal i as it shrinks.
Outer i
Inner j
Repeats
Prints
5
1..5
5
55555
3
1..3
3
333
1
1..1
1
1
The printed value is always the outer i; the inner loop only controls how many times it appears.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a String.repeat shortcut. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer down from 5, inner prints i exactly i times.
Java
public class InvertedRepeatingTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Output
55555
4444
333
22
1
How It Works
1. Outer sets digit and width.i starts at rows and moves toward 1 — both the printed value and the repeat count.
2. Inner repeats the digit.j runs from 1 to i, so i appears i times.
3. End the row. Call System.out.println() only after the inner loop finishes.
Example 2 — User Input Rows
Read the row count at runtime. Prefer hasNextInt() before nextInt() in real apps.
Java
import java.util.Scanner;
public class InvertedRepeatingTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
4444
333
22
1
How It Works
1. Same loop core. Only the source of rows changes from a literal to Scanner.
2. Entering 4 stops early. You get four rows ending at a single 1.
3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive height (tip below).
Safer input tip
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — String.repeat Shortcut
Build each row in one call once the i-times rule is clear.
Java
public class InvertedRepeatingString {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
System.out.println(String.valueOf(i).repeat(i));
}
}
}
Output
55555
4444
333
22
1
How It Works
1. Same outer loop.i still counts down from rows to 1.
2. Count equals digit.repeat(i) matches the nested inner loop that runs i times.
3. Learn loops first. Prefer Examples 1–2 in class; use repeat as a later shortcut (Java 11+).
Edge Cases & Pitfalls
Check these before calling the solution done.
Print value
Print i, not j
Printing j reprints an ascending digit run on each shrinking row, not a repeated-digit triangle.
Inner bound
Inner must run 1..i
Using rows..i instead reprints Program 10 (5, 44, 333), not this shape.
println inside
Do not put println inside the inner loop
That prints one digit per line and destroys the triangle.
Bad input
Validate with hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require rows >= 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(n²)
O(1)
String.repeat (Example 3)
O(n²)
O(n) per row string
Digits printed are n + (n − 1) + … + 1 = n(n + 1) / 2 — quadratic in n. Direct prints need only the loop variables; repeat builds a short-lived row string.
Remember
Key Takeaways
Rule: outer i = rows..1, inner j = 1..i, print i.
Shrink: each row drops one copy of a smaller digit.
Break the row: call println only after the inner loop finishes.
Complexity:O(n²) time; O(1) extra space with direct prints.
One line: print digit i exactly i times while i counts down from rows.
Frequently Asked Questions
An inverted repeating number triangle: for rows=5 you get 55555, 4444, 333, 22, 1.
The outer loop decreases i from rows 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.
System.out.print(i) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Program 9 counts up (1, 22, 333). Program 11 uses the same digit/count rule but counts down, so the widest row comes first (55555, 4444, 333).
Program 10 uses inner rows..i so repeats grow while the digit falls (5, 44, 333). Program 11 uses inner 1..i so digit and count both equal i (55555, 4444, 333).
Yes. System.out.println(String.valueOf(i).repeat(i)) prints a full row in one call. Nested loops are better for learning; repeat is a handy shortcut later.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.
🤔
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.