Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned decreasing triangle updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 values
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
Trace
Worked Walkthrough — rows = 5
Trace indent steps (i − 1), the number range rows..i, and the printed row.
i
Indent
Numbers
Printed row
5
4 × " "
5
5
4
3 × " "
5, 4
5 4
3
2 × " "
5, 4, 3
5 4 3
2
1 × " "
5 … 2
5 4 3 2
1
0
5 … 1
5 4 3 2 1
Total numbers = 1 + 2 + … + 5 = 15. Indent shrinks by one step each row.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a compact dry-run. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — indent loop plus descending number loop with %2d.
Java
public class RightAlignedDecreasing {
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(" ");
for (int j = rows; j >= i; j--)
System.out.printf("%2d", j);
System.out.println();
}
}
}
Output
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
How It Works
1. Outer loop descends.i runs from 5 down to 1 — first row is shortest.
2. Indent loop. Print " " for j = 1..i−1 to push the row right.
3. Number loop. Print j from rows down to i with %2d.
When i = 5: four indents then 5. When i = 1: no indent — 5 4 3 2 1.
Example 2 — Rows Input
Read rows at runtime. Same dual inner loops; bound uses rows.
Java
import java.util.Scanner;
public class RightAlignedDecreasingInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j < i; j++)
System.out.print(" ");
for (int j = rows; j >= i; j--)
System.out.printf("%2d", j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 3)
Enter rows: 3
3
3 2
3 2 1
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same core. Only the source of rows changes from a literal to user input.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Compact rows = 3
Same indent and number loops as Example 1 — smaller size for paper tracing.
Java
public class RightAlignedDecreasingSmall {
public static void main(String[] args) {
int rows = 3;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j < i; j++)
System.out.print(" ");
for (int j = rows; j >= i; j--)
System.out.printf("%2d", j);
System.out.println();
}
}
}
Output
3
3 2
3 2 1
How It Works
1. Same structure. Indent + descending numbers — only rows changes from 5 to 3.
2. Indent shrinks. First row has 2 indent steps; middle has 1; last has none.
3. Dry-run first. Trace i = 3..1 on paper before coding the full rows = 5 demo.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of numbers
If println is inside either inner loop, each value lands on its own line. Use print/printf for cells; println only after both inner loops.
Ascending outer
Upside-down shape
Using i = 1..rows grows the triangle from the top instead of starting with a single digit.
Wrong indent width
Drifted columns
Single spaces instead of " " break alignment with %2d.
No %2d
Crowded digits
Plain print(j) makes multi-digit values crowd earlier columns.
rows = 1
Single value
Output is just 1 with no leading spaces.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Right-aligned decreasing (Examples 1–3)
O(n²)
O(1)
Total numbers = n(n+1)/2; indent work is also O(n²) overall. Only loop counters are stored.
Remember
Key Takeaways
Rule: for i = rows..1, indent i−1 steps, then print rows..i.
Match widths: two-space indent steps pair with %2d number columns.
Break the row: call println only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: indent, then print rows..i with %2d, then println().
Frequently Asked Questions
A right-aligned decreasing triangle: the first row prints 5, then 5 4, then 5 4 3, and so on until 5 4 3 2 1.
An indentation loop prints two spaces while j < i before the number loop runs, pushing shorter rows to the right.
The format specifier reserves 2 columns per number (right-aligned), keeping columns stable in the console output.
The number loop runs for (j = rows; j >= i; j--), so every row begins at rows and counts down to the current i.
Program 35 uses a continuous counter k. Program 36 restarts from rows on each row with a separate indentation loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
🤔
Did you know?
Each row starts from rows and counts down to i. An indentation loop prints two spaces per step (j = 1..i-1), then System.out.printf("%2d", j) keeps number columns aligned.