PHP Number Triangle Pattern (Increasing Jump)

Beginner
5 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, then jumps forward with a step that shrinks after every print.

Remember
Rule: print i, then add shrinking m (start m = n-1)

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

In PHP print $i first, set $m = $rows - 1 and $k = $i + $m, then in the inner loop print $k, do $m--, and update $k = $k + $m. Program 22 continues with odd-length number rows.

How to Solve It

Outer loop from 1 to $rows. Print $i, seed $m and $k, then print $i - 1 more jumped values. End each row with echo PHP_EOL.

MethodIdeaBest for
Decreasing stepStart at $i; add $m, then $m--Learning, interviews, exams
Custom stepSeed $m with a fixed value instead of $rows - 1Tighter or wider jumps

Pseudocode

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

Cheat sheet

GoalPattern
Pick row startfor ($i = 1; $i <= $rows; $i++)
Print row headecho $i . " ";
Seed step & next$m = $rows - 1; $k = $i + $m;
Jump loopfor ($j = 1; $j < $i; $j++) then $m--; $k = $k + $m;
End the rowecho PHP_EOL;
Custom step$m = 3; instead of $rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $i . " " / echo $k . " "Stays on the same lineEach number in the row
echo PHP_EOLEnds the current lineAfter the jump loop

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

Live Preview

Change the row count and the jump triangle updates instantly.

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

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

Worked Walkthrough

Trace three rows when $rows = 5 ($m starts at 4 each row) — watch the jumps shrink.

$iFirst jumpsPrinted row
1(none)1
22 + 4 = 62 6
33+4=7, then 7+3=103 7 10
44+4=8, 8+3=11, 11+2=134 8 11 13

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

PHP Programs

Three complete programs: fixed height, CLI input, and a custom initial step. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Print $i, then jump with $m = 4 shrinking after each value.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    echo $i . " ";

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

    for ($j = 1; $j < $i; $j++) {
        echo $k . " ";
        $m--;
        $k = $k + $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Print the row head. Each row begins with $i — that is always the first number on the line.

2. Seed the jump. $m = $rows - 1 and $k = $i + $m set the first jumped value.

3. Shrink and add. After each print, $m-- then $k = $k + $m so later jumps get smaller.

Example 2 — User Input

Read the row count from the CLI. Prefer is_numeric in real apps (shown in the tip below).

PHP
<?php
echo "Enter the number of rows: ";
$rows = (int) trim(fgets(STDIN));

for ($i = 1; $i <= $rows; $i++) {
    echo $i . " ";

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

    for ($j = 1; $j < $i; $j++) {
        echo $k . " ";
        $m--;
        $k = $k + $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Read rows. Ask for a row count, then trim(fgets(STDIN)) and cast to int.

2. Scale the step. $m = $rows - 1 makes the first jump match the triangle height.

3. Safer input tip. Bare (int) casts turn letters into 0. Prefer:

Safer input
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input) || (int) $input < 1) {
    echo "Enter a positive whole number." . PHP_EOL;
    exit(1);
}
$rows = (int) $input;

Example 3 — Custom Initial Step

Keep $rows = 5 but start each row with $m = 3 for tighter jumps.

PHP
<?php
$rows = 5;

for ($i = 1; $i <= $rows; $i++) {
    echo $i . " ";

    $m = 3;
    $k = $i + $m;

    for ($j = 1; $j < $i; $j++) {
        echo $k . " ";
        $m--;
        $k = $k + $m;
    }

    echo PHP_EOL;
}
?>

How It Works

1. Same loop shape. Outer and inner structure match Example 1.

2. Different seed. $m = 3 replaces $rows - 1 (which would be 4).

3. Tighter jumps. Smaller starting steps pull later values closer together — compare row 2: 2 5 here vs 2 6 in Example 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

forget m--

Forgetting $m--

Every jump stays the same size. Decrease $m after each print so later values tighten.

wrong bound

$j <= $i instead of $j < $i

That prints one extra jumped value. The inner loop runs $i - 1 times after the head.

PHP_EOL inside

echo PHP_EOL inside the jump loop

That puts each number on its own line. Call echo PHP_EOL only after the row finishes.

rows = 1

Single number

Output is just 1 — the jump loop never runs.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

spaces

Keep the trailing space

The classic sample uses echo $i . " " / echo $k . " " so numbers stay spaced and readable.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input / custom step (Examples 1–3)O(n²)O(1)

There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.

Key Takeaways

  • Rule: print $i, then jump with a shrinking $m that starts at $rows - 1.
  • Update order: print $k, then $m--, then $k = $k + $m.
  • echo vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 22 prints odd-length number rows.

One line: print $i, then add a shrinking $m to build the rest of the row.

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.
With 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.
echo $i . " " stays on the same line with a trailing space. echo PHP_EOL ends the current line. Numbers use echo; the row break uses echo PHP_EOL 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 trim(fgets(STDIN)) and check is_numeric($input) before casting to int so bad input does not produce unexpected results.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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

Continue with rows whose lengths grow by two each time.

Program 22 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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