Java Incremental Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned incremental triangle prints a continuous counter 1, 2, 3, … across rows, padding the left with spaces and using fixed-width columns so values stay aligned.

Remember
Rule: k = 1; for i from 1 to rows,
      for j from rows down to 1:
        if j > i: print 3 spaces
        else: printf("%3d", k++)

              1
           2  3
        4  5  6
     7  8  9 10
 11 12 13 14 15     ← rows = 5

Unlike Program 34 (per-cell formula i + j) and Program 30 (descending digits), here one counter k runs across the whole triangle.

How to Solve It

Grow i from 1 to rows; in a fixed-width descending loop, print spaces while j > i, otherwise print k++ with %3d.

MethodIdeaBest for
Counter + printfk++ with %3d; spaces while j > iLearning, interviews, exams
User-input rowsSame logic; read height with ScannerWhen triangle size must vary

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to 1:
        if j > i: print "   "
        else: print k (width 3); k = k + 1
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Fixed-width columnfor (int j = rows; j >= 1; j--)
Leading spacesif (j > i) System.out.print(" ");
Print counterSystem.out.printf("%3d", k++);
End the rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.print / printfStays on the same lineSpaces and each formatted number
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 incremental 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
              1
           2  3
        4  5  6
     7  8  9 10
 11 12 13 14 15

Worked Walkthrough — rows = 5

Trace leading space groups (rows − i), numbers taken from k, and the printed row.

iSpacesNumbers from kPrinted row
14 × 311
23 × 32, 32 3
32 × 34, 5, 64 5 6
41 × 37 … 107 8 9 10
5011 … 1511 12 13 14 15

Total numbers = 1 + 2 + … + 5 = 15. Space groups shrink by one 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 — continuous k with %3d and three-space padding.

Java
public class RightAlignedIncremental {
    public static void main(String[] args) {
        int k = 1;

        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= 1; j--) {
                if (j > i)
                    System.out.print("   ");
                else
                    System.out.printf("%3d", k++);
            }
            System.out.println();
        }
    }
}

How It Works

1. Counter outside. k starts at 1 before the outer loop and never resets.

2. Fixed-width inner loop. j runs from 5 down to 1 — spaces while j > i.

3. Formatted print. printf("%3d", k++) prints the next value in a 3-column field.

When i = 1: four space groups then 1. When i = 5: no spaces — 11 … 15.

Example 2 — Rows Input

Read rows at runtime. Same counter and formatting; inner loop uses rows as width.

Java
import java.util.Scanner;

public class RightAlignedIncrementalInput {
    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;

        int k = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= 1; j--) {
                if (j > i)
                    System.out.print("   ");
                else
                    System.out.printf("%3d", 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 — Compact rows = 3

Same counter and spacing as Example 1 — smaller size for paper tracing.

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

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

How It Works

1. Same structure. Continuous k and three-space padding — only rows changes from 5 to 3.

2. Spaces shrink. Row 1 has 2 space groups; 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 numbers

If println is inside the inner loop, each value lands on its own line. Use print/printf for cells; println only after the inner loop.

Reset k

Restarted sequence

Declaring k = 1 inside the outer loop restarts the count every row instead of continuing.

Wrong space width

Drifted columns

Single spaces instead of " " break alignment with %3d.

No %3d

Crowded digits

Plain print(k++) makes two-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 incremental (Examples 1–3)O(n²)O(1)

Total numbers = n(n+1)/2; each row also walks a fixed-width loop of size n — still quadratic. Only counters are stored.

Key Takeaways

  • Rule: continuous k++ across rows; spaces while j > i; print with %3d.
  • Keep k outside: declare k = 1 before the outer loop so the sequence never resets.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: spaces if j > i, else printf("%3d", k++), then println().

Frequently Asked Questions

Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
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?

A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and System.out.printf("%3d", k++) keeps columns aligned as values grow past single digits.

Next: Right-Aligned Decreasing Triangle

Move on to the right-aligned decreasing number triangle in the Java number-pattern series.

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