Java Number Triangle Pattern (Increasing Jump)

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

What Is This Pattern?

An increasing jump number triangle starts each row at the row index i, then jumps forward with a step m that shrinks after every print.

Remember
Rule: print i, then for the rest of the row
      add a decreasing step m (start m = rows - 1)

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

Unlike Program 20 (one continuous k++), this pattern resets a local step m on every row and shrinks it after each jump.

How to Solve It

Print i first, then walk the rest of the row with m-- and k = k + m.

MethodIdeaBest for
Decreasing stepm = rows - 1; print k, then m--, k += mLearning, interviews, exams
Custom stepSame loops; set initial m yourselfTighter or wider jumps

Pseudocode

Pseudocode
for i from 1 to rows:
    print i
    m = rows - 1
    k = i + m
    for j from 1 to i - 1:
        print k
        m = m - 1
        k = k + m
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (int i = 1; i <= rows; i++)
Print row startSystem.out.print(i + " ");
Initial stepint m = rows - 1;
First jump valueint k = i + m;
Next jumpm--; k = k + m; after each k print
End the rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach i and each k with a trailing space
System.out.printlnEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the jump number triangle updates instantly.

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

Live result rows = 5 · 15 numbers
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15

Worked Walkthrough — rows = 4

Trace each row’s start, initial m, and the jumped values.

iStart + jumpsPrinted row
11 (no jumps)1
22, then m=3 → 52 5
33, 6 (m=3), 8 (m=2)3 6 8
44, 7, 9, 104 7 9 10

Total numbers: 1 + 2 + 3 + 4 = 10 = n(n + 1) / 2 — that is why time is O(n²).

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a custom initial step. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — print i, then jump with m = 4.

Java
public class JumpNumberTriangle {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            System.out.print(i + " ");

            int m = 4;
            int k = i + m;

            for (int j = 1; j < i; j++) {
                System.out.print(k + " ");
                m--;
                k = k + m;
            }

            System.out.println();
        }
    }
}

How It Works

1. Print the row start. Each row begins with i.

2. Set the first jump. m = 4 (rows - 1) and k = i + m.

3. Shrink the step. After printing k, do m-- then k = k + m for the next value.

When i = 3: print 3, then k = 7, m-- to 3, k = 10 → 3 7 10.

Example 2 — Rows Input

Read rows at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

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

        for (int i = 1; i <= rows; i++) {
            System.out.print(i + " ");

            int m = rows - 1;
            int k = i + m;

            for (int j = 1; j < i; j++) {
                System.out.print(k + " ");
                m--;
                k = k + m;
            }

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

How It Works

1. Prompt and read. Ask for a height, then store it with sc.nextInt().

2. Scale the step. m = rows - 1 each row so the initial jump matches triangle height.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Example 3 — Custom Initial Step m = 3

Keep rows = 4 but start each row with m = 3 (here equal to rows - 1, shown as an override).

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

        for (int i = 1; i <= rows; i++) {
            System.out.print(i + " ");

            int m = 3;
            int k = i + m;

            for (int j = 1; j < i; j++) {
                System.out.print(k + " ");
                m--;
                k = k + m;
            }

            System.out.println();
        }
    }
}

How It Works

1. Same loops. Print i, then jump with m-- and k = k + m.

2. Override the start. Only the initial m changes — try other values to tighten or widen jumps.

3. Same shape. Row lengths stay 1, 2, 3, 4; only the jump sizes change when m differs from rows - 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

Wrong m

Step not reset

Reset m = rows - 1 at the start of every row. Reusing a leftover m breaks later rows.

Order

m-- before add

After printing k, decrease m first, then do k = k + m. Reversing the order uses the wrong step.

println inside

Column of numbers

If println is inside the inner loop, each number lands on its own line. Use print for numbers; println only after the inner loop.

rows = 1

Single 1

Output is just 1 — the inner loop never runs. A good sanity check.

rows ≤ 0

Empty output

The outer loop never runs. Validate and require rows ≥ 1.

Bad input

Use hasNextInt

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

Time and Space Complexity

ProgramTimeExtra space
Decreasing step (Examples 1–3)O(n²)O(1)

Total prints: 1 + 2 + … + n = n(n + 1) / 2 — still quadratic in n. Only a few scalars (i, m, k) are stored.

Key Takeaways

  • Rule: print i, then jump with a decreasing step m.
  • Reset m: set m = rows - 1 at the start of every row.
  • Update order: after printing k, do m-- then k = k + m.
  • Complexity: O(n²) time; O(1) extra space.

One line: print i, then add a shrinking step m for the rest of the row, then println().

Frequently Asked Questions

Because the step variable m starts at rows − 1 and decreases after each printed number. Each next value is computed by adding the current m, so the jumps shrink across the row.
m is the step size used to compute the next printed number in the row. It decreases each time, changing the increment between consecutive outputs.
For rows = 5, row 2 prints i = 2 first. Then m = 4 and k = i + m = 6 — the inner loop prints k once, giving 2 6.
System.out.print stays on the same line with a trailing space. System.out.println ends the current line. Numbers use print; the row break uses println after the inner loop.
Total prints are 1+2+…+n = n(n+1)/2 — row i prints exactly i values.
Yes. Set m to a custom value instead of rows − 1 — see Example 3. Smaller steps produce tighter jumps.
O(n²) for n rows because total digit prints equal n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt(), require rows ≥ 1, and reject non-numeric input — see Example 2 notes.

Did you know?

Each row starts at i, then adds a decreasing step m to compute the next value. As m shrinks after each print, the jumps get smaller toward the end of the row — total prints still equal n(n+1)/2 for n rows.

Next: Odd-Length Number Rows

Move on to odd-length number rows in the Java number-pattern series.

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