Java Descending Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned descending number triangle pads each row with leading spaces, then prints digits from i down to 1 — so the triangle grows toward the right edge.

Remember
Rule: for i from 1 to rows,
      for j from rows down to 1:
        print space if j > i else print j

    1
   21
  321
 4321
54321     ← rows = 5

Unlike Program 3 (left-aligned reverse descending, no padding), here a fixed-width inner loop fills unused columns with spaces.

How to Solve It

Walk i from 1 to rows; in one fixed-width loop, print a space when j > i, otherwise print j.

MethodIdeaBest for
Fixed-width if/elseSpace if j > i; else digit jLearning, interviews, exams
Ternary formSame logic with j > i ? " " : jShorter code once the idea clicks

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from rows down to 1:
        print (" " if j > i else j)
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Fixed-width columnfor (int j = rows; j >= 1; j--)
Space or digitif (j > i) System.out.print(" "); else System.out.print(j);
End the rowSystem.out.println();
TernarySystem.out.print(j > i ? " " : j);

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach space or digit
System.out.printlnEnds the current lineAfter the inner loop finishes

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

Live Preview

Change the row count and the right-aligned triangle updates instantly.

Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · width 5
    1
   21
  321
 4321
54321

Worked Walkthrough — rows = 5

Trace leading spaces (rows − i) and the descending digit group for each i.

iSpacesDigitsPrinted row
1411
232121
32321321
4143214321
505432154321

Each row prints exactly rows characters — total work is O(n²) for n rows.

Java Programs

Three complete programs: fixed rows = 5, Scanner input with ternary form, and a compact dry-run. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded width — if/else in one fixed-width descending loop.

Java
public class RightAlignedDescending {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= 1; j--) {
                if (j > i)
                    System.out.print(" ");
                else
                    System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop grows. i runs from 1 to 5 — one right-aligned row per iteration.

2. Inner loop is fixed-width. j always runs from 5 down to 1.

3. Space or digit. Print a space while j > i; otherwise print j.

When i = 3: two spaces + 321. When i = 5: no spaces — 54321.

Example 2 — Rows Input

Read rows at runtime. Ternary replaces if/else; inner loop uses rows as width.

Java
import java.util.Scanner;

public class RightAlignedInput {
    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 = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--)
                System.out.print(j > i ? " " : j);

            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Generalize bounds. Inner loop uses rows instead of hard-coded 5.

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 if/else structure as Example 1 — smaller size for paper tracing.

Java
public class RightAlignedSmall {
    public static void main(String[] args) {
        int rows = 3;

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (j > i) System.out.print(" ");
                else System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Fixed-width descending loop — only rows changes from 5 to 3.

2. Spaces shrink. Row 1 has 2 leading spaces; row 2 has 1; row 3 has none.

3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of digits

If println is inside the inner loop, each character lands on its own line. Use print for spaces/digits; println only after the inner loop.

Skip spaces

Left-aligned

Omitting the space branch collapses the pattern into Program 3’s left-aligned triangle.

Wrong test

Inverted padding

Using j < i for spaces (instead of j > i) pads the wrong side and breaks the descending digits.

rows = 1

Single digit

Output is just 1 — no leading spaces.

rows ≤ 0

Empty output

The outer loop never runs — print nothing or show a message.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Right-aligned descending (Examples 1–3)O(n²)O(1)

n rows, each printing n characters — quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: for each i, loop j = rows..1 — space if j > i, else print j.
  • Fixed width: the inner loop always runs rows times so columns stay aligned.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for j = rows..1, print space if j > i else j, then println().

Frequently Asked Questions

Leading spaces are printed while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs j from rows down to 1. When j <= i, it prints j — naturally producing i..1 on each row.
Running j from rows down to 1 on every row keeps column alignment. Spaces fill positions where j > i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
Replace 5 with rows in the inner loop bound — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
Yes — System.out.print(j > i ? " " : j) compacts the if/else logic.

Did you know?

This pattern uses a fixed column width (rows). For each row i, the inner loop prints spaces while j > i, then prints digits in descending order — producing a right-aligned triangle.

Next: Number-Star Diamond Pattern

Move on to the number-star diamond pattern in the Java number-pattern series.

Program 31 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