The outer loop counts down so the first row is widest. The next pattern, Program 2, keeps the same length idea but shifts the start digit leftward instead.
Approach
How to Solve It
Outer loop from rows down to 1. Inner loop from 1 to current i, printing each digit. End the line after the inner loop.
Method
Idea
Best for
Nested loops
Outer down, inner 1..i
Learning, interviews, exams
StringBuilder
Append digits, then println the row
When you want one print per row
Pseudocode
Pseudocode
for i from rows down to 1:
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Outer (shrink width)
for (int i = rows; i >= 1; i--)
Inner (print 1..i)
for (int j = 1; j <= i; j++) System.out.print(j);
End the row
System.out.println();
Spaced digits
System.out.print(j + " ");
One print per row
Append to StringBuilder, then println(row)
Ascending instead
Outer i = 1..rows (same inner)
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit j
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 triangle updates instantly.
Whole numbers from 1 to 12. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
12345
1234
123
12
1
Trace
Worked Walkthrough
Trace three outer values when rows = 5 — watch the row width shrink while digits still start at 1.
Outer i
Inner j
Prints
5
1..5
12345
3
1..3
123
1
1..1
1
Digits always begin at 1; only the stopping value i moves down as the outer loop counts down.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a StringBuilder row builder. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded height — outer down from 5, inner prints 1..i.
Java
public class DescendingNumberTriangle {
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(j);
}
System.out.println();
}
}
}
Output
12345
1234
123
12
1
How It Works
1. Outer shrinks the width.i starts at rows and moves toward 1 — that is how many digits each row gets.
2. Inner prints ascending digits.j runs from 1 to i with System.out.print(j).
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 DescendingNumberTriangleInput {
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(j);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
1234
123
12
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 — StringBuilder Row Builder
Append each digit, then print the full row once with println.
Java
public class DescendingNumberString {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
StringBuilder row = new StringBuilder();
for (int j = 1; j <= i; j++) {
row.append(j);
}
System.out.println(row);
}
}
}
Output
12345
1234
123
12
1
How It Works
1. Same bounds. Outer i = rows..1 and inner j = 1..i match Example 1.
2. Build, then print.row.append(j) collects digits; println(row) prints the whole line.
3. Same shape. Useful when exams want one print per row — loop bounds stay visible either way.
Edge Cases & Pitfalls
Check these before calling the solution done.
Outer direction
Count down with i--
Counting up from 1 to rows prints an ascending triangle (1, 12, 123), not this shape.
println inside
Do not put println inside the inner loop
That prints one digit per line and destroys the triangle.
Missing println
Always end the row
Omitting println glues every digit onto a single endless line.
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)
StringBuilder (Example 3)
O(n²)
O(n) per row buffer
Digits printed are n + (n − 1) + … + 1 = n(n + 1) / 2 — quadratic in n. Direct prints need only the loop variables; StringBuilder adds a short-lived row buffer.
Remember
Key Takeaways
Rule: outer i = rows..1, inner j = 1..i, print j.
Shrink: each row drops one digit from the right while still starting at 1.
Break the row: call println only after the inner loop finishes.
Complexity:O(n²) time; O(1) extra space with direct prints.
One line: start at full width, print 1..i, then drop one digit each next row.
Frequently Asked Questions
A descending number triangle: for rows=5 you get 12345, 1234, 123, 12, 1.
Counting down makes the first row the longest. for (i = rows; i >= 1; i--) sets i to the full width first, then shrinks by one each line.
System.out.print(j) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Change the outer loop to for (i = 1; i <= rows; i++). Keep the inner loop as for (j = 1; j <= i; j++) System.out.print(j).
Program 1 prints 1..i with outer counting down (12345, 1234…). Program 2 starts each row at i and prints i..rows (12345, 2345…).
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt(), require rows ≥ 1, and reject non-numeric input — see Example 2.
One row prints a single digit 1.
🤔
Did you know?
Row i prints digits 1 through i. The outer loop counts down from rows, so the first line is longest and each row shortens by one digit — still O(n²) total prints.