Print Inverted Number Triangle in Java
Problem
An inverted number triangle keeps the same row-relative counting as a plain number triangle, but starts each row at its widest and narrows it down one row at a time.
Given a number of rows, print a left-aligned triangle where the widest row comes first and each row still counts 1 up to its own width.
Java Program
public class InvertedNumberTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) { // widest row first
StringBuilder line = new StringBuilder();
for (int j = 1; j <= i; j++) {
if (j > 1) line.append(" ");
line.append(j);
}
System.out.println(line);
}
}
}Output
Core Logic
Counting the outer loop downward instead of upward keeps every row's own 1-to-width counting rule unchanged, while shrinking how wide each row gets.
- 1The outer loop runs
ifromndown to 1, so the first row printed is the widest. - 2The inner loop runs
jfrom 1 toi, printingjat each step — the exact same row-content rule as the plain number triangle. - 3Because
ishrinks by one each time, every subsequent row has one fewer number than the last. - 4The first row is
1throughn, and the final row is just1.
n = 5, the first row prints 1 2 3 4 5, and each row after that drops its final number, ending at a single 1.Key Point: Only the outer loop's direction changes here — the inner loop's own 1-to-i counting rule is identical to the plain number triangle, it just runs for fewer values as the rows go on.
Why: The total count of numbers printed is still n+(n-1)+...+1, proportional to n², the same total work as the non-inverted version.
Key Concepts
Approach 2: Java 8
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class InvertedNumberTriangleStream {
public static void main(String[] args) {
int n = 5;
IntStream.rangeClosed(1, n)
.map(k -> n + 1 - k) // widest row first
.mapToObj(i -> IntStream.rangeClosed(1, i)
.mapToObj(String::valueOf)
.collect(Collectors.joining(" ")))
.forEach(System.out::println);
}
}
Output
Core Logic
Reusing the plain number triangle's exact per-row formula, but feeding it row indices in descending order instead of ascending, shrinks each row instead of growing it.
- 1
IntStream.rangeClosed(1, n).map(k -> n + 1 - k)produces the row indicesndown to1, the reverse of the plain triangle's ascending stream. - 2For each row
i, an innerIntStream.rangeClosed(1, i)still generates1throughi— the identical row-content rule as the plain number triangle. - 3
.mapToObj(String::valueOf)converts each value to a String for joining. - 4
Collectors.joining(" ")joins that row's numbers beforeforEachprints it.
5, giving "1 2 3 4 5"; the last is 1, giving just "1".Key Point: Only the row-index stream's direction changes here — the exact same inner-stream mapping function from the plain triangle is reused unmodified, it just runs for fewer values as the outer stream progresses.
Why: The nested streams still produce n+(n-1)+...+1 values total, proportional to n², regardless of visiting order.