Java Repeating Number Triangle Pattern (Descending)
Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A descending repeating number triangle starts with one copy of the peak digit, then repeats a smaller digit more times each row — so you see 5, 44, 333, and so on.
Remember
Rule: outer i = rows..1; inner j = rows..i; print i
5
44
333
2222
11111 ← rows = 5
Unlike Program 9 (digit i repeats i times upward) or Program 11 (same digit/count, but widest first), this pattern grows the repeat count while the digit falls.
Approach
How to Solve It
Outer loop from rows down to 1. Inner loop from rows down to current i, printing i each time. End the line after the inner loop.
Method
Idea
Best for
Nested loops
Outer down, inner rows..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 rows down to i:
print i
print newline
Cheat sheet
Goal
Pattern
Outer (digit falls)
for (int i = rows; i >= 1; i--)
Inner (repeat count)
for (int j = rows; j >= i; j--) System.out.print(i);
End the row
System.out.println();
Repeats on row i
rows - i + 1
Shortcut
String.valueOf(i).repeat(rows - i + 1)
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 descending 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
5
44
333
2222
11111
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch the digit fall while the repeat count grows.
Outer i
Inner j
Repeats
Prints
5
5..5
1
5
3
5..3
3
333
1
5..1
5
11111
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 from 5 down to i, print i.
Java
public class DescendingRepeatingTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = rows; j >= i; j--) {
System.out.print(i);
}
System.out.println();
}
}
}
Output
5
44
333
2222
11111
How It Works
1. Outer sets the digit.i starts at rows and moves toward 1 — that value is what you print.
2. Inner sets the count.j runs from rows down to i, so the digit appears rows - i + 1 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 DescendingRepeatingTriangleInput {
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 = rows; j >= i; j--) {
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
4
33
222
1111
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 1111.
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 rows - i + 1 count is clear.
Java
public class DescendingRepeatingString {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
System.out.println(String.valueOf(i).repeat(rows - i + 1));
}
}
}
Output
5
44
333
2222
11111
How It Works
1. Same outer loop.i still counts down from rows to 1.
2. Count formula.rows - i + 1 is exactly how many times the nested inner loop would run.
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 a descending digit run, not a repeated-digit triangle.
Inner bound
Inner must run rows..i
Using 1..i instead reprints Program 11 (55555, 4444, 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 1 + 2 + … + n = 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 = rows..i, print i.
Growth: each row adds one more 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 rows - i + 1 times while i counts down.
Frequently Asked Questions
A descending repeating number triangle: for rows=5 you get 5, 44, 333, 2222, 11111.
The outer loop decreases i from rows to 1. The inner loop runs (rows - i + 1) times, so the repeat count grows as the digit shrinks.
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 grows upward: row i repeats digit i exactly i times (1, 22, 333). Program 10 starts with one copy of the top digit and increases repeats as the digit decreases (5, 44, 333).
Program 11 also counts down but repeats i exactly i times (55555, 4444, 333…). Program 10 uses an inner bound of rows..i so repeats grow while the digit falls.
Yes. System.out.println(String.valueOf(i).repeat(rows - i + 1)) 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 rows - i + 1 times. The outer loop counts down from rows, so the first row shows one copy of the top digit and the last row repeats 1 for rows times — still O(n²) total prints.