Java Decreasing Number Triangle Pattern (Right-Aligned)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A right-aligned decreasing triangle prints each row from rows down to i, with indentation spaces so shorter rows sit on the right.

Remember
Rule: for i from rows down to 1,
      indent (i-1) times with "  "
      print j from rows down to i with %2d

         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1     ← rows = 5

Unlike Program 35 (continuous counter across rows), here every row restarts at rows and uses a separate indent loop.

How to Solve It

Descend i from rows to 1; indent with two spaces, then print j = rows..i with %2d.

MethodIdeaBest for
Indent + number loopsSpaces while j < i; then rows..i with %2dLearning, interviews, exams
User-input rowsSame dual-loop logic; read height with ScannerWhen triangle size must vary

Pseudocode

Pseudocode
for i from rows down to 1:
    for j from 1 to i - 1:
        print "  "
    for j from rows down to i:
        print j (width 2)
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = rows; i >= 1; i--)
Indentfor (int j = 1; j < i; j++) System.out.print(" ");
Print numbersfor (int j = rows; j >= i; j--) System.out.printf("%2d", j);
End the rowSystem.out.println();
Column width%2d matches two-space indent steps

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.print / printfStays on the same lineIndent spaces and each formatted number
System.out.printlnEnds the current lineAfter both inner loops finish

Print characters without a newline, then end the row once.

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 result rows = 5 · 15 values
         5
       5 4
     5 4 3
   5 4 3 2
 5 4 3 2 1

Worked Walkthrough — rows = 5

Trace indent steps (i − 1), the number range rows..i, and the printed row.

iIndentNumbersPrinted row
54 × " "55
43 × " "5, 45 4
32 × " "5, 4, 35 4 3
21 × " "5 … 25 4 3 2
105 … 15 4 3 2 1

Total numbers = 1 + 2 + … + 5 = 15. Indent shrinks by one step each row.

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();
        }
    }
}

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Palindrome Number Triangle

Move on to the palindrome number triangle in the Java number-pattern series.

Program 37 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful