Java Increasing Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned increasing number triangle prints 1, then 1 2, then 1 2 3, up to rows — with leading spaces so shorter rows sit to the right.

Remember
Rule: for i from 1 to rows,
      print (rows - i) spaces, then format("%2d", k) for k = 1..i

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

Unlike Program 42 (hollow square grid), here each row grows by one number and pads from the left.

How to Solve It

Walk i from 1 to rows; print rows − i spaces, then numbers 1..i with %2d.

MethodIdeaBest for
Spaces + numbersSpace loop for indent; number loop for 1..iLearning, interviews, exams
Left-alignedSkip the space loop; print only 1..iWhen you want a left-growing triangle

Pseudocode

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

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Leading spacesfor (int j = rows; j > i; j--) print(" ")
Print numbersSystem.out.format("%2d", k); for k = 1..i
End the rowSystem.out.println();
Spaces on row irows - i

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.print / formatStays on the same lineSpaces and each number
System.out.printlnEnds the current lineAfter spaces and numbers finish

Print values 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 · 15 numbers
     1
    1 2
   1 2 3
  1 2 3 4
 1 2 3 4 5

Worked Walkthrough — rows = 5

Trace spaces (rows − i) and numbers (1..i) for each row.

iSpacesNumbersPrinted row
1411
231 21 2
321 2 31 2 3
411..41 2 3 4
501..51 2 3 4 5

Total numbers = 1 + 2 + 3 + 4 + 5 = 15 — the triangular number n(n+1)/2.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a left-aligned variant. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded size — space loop for indent, number loop with %2d.

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

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j > i; j--)
                System.out.print(" ");
            for (int k = 1; k <= i; k++)
                System.out.format("%2d", k);
            System.out.println();
        }
    }
}

How It Works

1. Outer loop. i is both the row index and how many numbers to print.

2. Space loop. Prints rows − i single spaces so the row shifts right.

3. Number loop. format("%2d", k) prints 1..i in fixed-width columns.

When i = 1: four spaces + 1. When i = 3: two spaces + 1 2 3.

Example 2 — Rows Input

Read rows at runtime. Same space-and-number core.

Java
import java.util.Scanner;

public class RightAlignedNumberTriangleInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();
        if (rows < 1) return;

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j > i; j--)
                System.out.print(" ");
            for (int k = 1; k <= i; k++)
                System.out.format("%2d", k);
            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 — Left-Aligned Triangle

Drop the space loop — numbers alone create a left-growing triangle.

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

        for (int i = 1; i <= rows; i++) {
            for (int k = 1; k <= i; k++)
                System.out.format("%2d", k);
            System.out.println();
        }
    }
}

How It Works

1. Same numbers. Still print k = 1..i with %2d.

2. No indent. Without the space loop, every row starts at the left.

3. Compare tip. Put Examples 1 and 3 side by side to see what the space loop changes.

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/format for cells; println only after the row.

No spaces

Left-aligned by accident

Skipping the space loop removes the right alignment — which is Example 3 on purpose.

No %2d

Crowded columns

Plain print(k) makes two-digit values crowd earlier columns.

No println

One long line

Forgetting println() after the row glues every number onto one endless line.

rows = 1

Single number

Output is just one formatted 1 on a single line.

Bad input

Use hasNextInt

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

Time and Space Complexity

ProgramTimeExtra space
Right / left triangle (Examples 1–3)O(n²)O(1)

Total numbers = n(n+1)/2 — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: print rows − i spaces, then numbers 1..i with %2d.
  • Indent first: the space loop creates the right alignment.
  • Break the row: call println only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row, print rows − i spaces, then format("%2d", k) for k = 1..i, then println().

Frequently Asked Questions

Each row prints leading spaces before the numbers. As i grows, the space count (rows - i) shrinks, pushing rows to align on the right.
The outer loop sets the row index i. An inner loop prints k from 1 to i on every row.
for (j = rows; j > i; j--) prints rows-i spaces before the numbers, creating the right alignment.
Remove the leading-space loop. Printing only numbers 1..i naturally left-aligns the triangle — see Example 3.
%2d prints each number in a two-character field so columns stay aligned when values reach two digits.
O(n²) for n rows because total printed numbers are 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
You get a single row with just the number 1 (with optional leading spaces depending on alignment).

Did you know?

Each row prints numbers 1..i and leading spaces push the row to the right — fewer spaces as i grows, which creates the right-aligned triangle.

Next: Number Diamond Pattern

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

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