PHP Sequential Number Triangle Pattern (Narrowing)

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

What Is This Pattern?

A decreasing continuous number triangle prints values from a shared counter $k that never resets, while each row prints one fewer number than the row above.

Remember
Rule: row i prints (rows - i + 1) values of k++

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

In PHP the inner loop runs from $rows down to $i, so the first row is the longest. Use printf("%3d", $k++) to keep columns aligned past single digits.

How to Solve It

One nested-loop idea with a shared counter and format width.

MethodIdeaBest for
Counter + shrinkPrint $k++ while row length shrinksLearning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to i:
        print k (width 3), then k = k + 1
    print newline

Cheat sheet

GoalPattern
Shrink each rowfor ($i = 1; $i <= $rows; $i++)
Row lengthfor ($j = $rows; $j >= $i; $j--)
Next numberprintf("%3d", $k++);
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%3d", $k++)Stays on the same lineEach formatted number
echo PHP_EOLEnds the current lineAfter the inner loop

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

Live Preview

Change the row count and the shrinking continuous triangle updates instantly.

Whole numbers from 1 to 8 (width-3 columns stay readable). Tap a chip or type a value — the preview redraws as you go.

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

Worked Walkthrough — $rows = 4

Trace how many values each row takes from counter $k.

$iCount / valuesPrinted row
14 / 1..41 2 3 4
23 / 5..75 6 7
32 / 8 98 9
41 / 1010

Row length is $rows - $i + 1. Counter $k ends at n(n+1)/2.

PHP Programs

Three complete programs: fixed height, CLI input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Shrinking inner loop prints continuous $k++ values with width-3 formatting.

PHP
<?php
$rows = 5;
$k = 1;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        printf("%3d", $k++);

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i grows from 1 to 5 — each pass shortens the row by one.

2. Inner loop. $j runs from $rows down to $i, so row 1 prints 5 values and row 5 prints 1.

3. Continuous $k. $k lives outside the loops and climbs from 1 to 15 without resetting.

Example 2 — User Input

Read the row count from STDIN; the inner loop uses $rows as the starting width.

PHP
<?php
echo "Enter rows: ";
$input = trim(fgets(STDIN));

if (!is_numeric($input) || (int) $input < 1) {
    echo "Please enter a positive whole number." . PHP_EOL;
    exit(1);
}

$rows = (int) $input;
$k = 1;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        printf("%3d", $k++);

    echo PHP_EOL;
}

How It Works

1. Validate rows. Check is_numeric; require $rows >= 1.

2. Same counter. Three rows print values 1 through 6 with shrinking width.

Example 3 — Compact $rows = 3

A smaller fixed demo — same continuous counter, easier to trace by hand.

PHP
<?php
$rows = 3;
$k = 1;

for ($i = 1; $i <= $rows; $i++) {
    for ($j = $rows; $j >= $i; $j--)
        printf("%3d", $k++);

    echo PHP_EOL;
}

How It Works

1. Same rules. Inner loop length is $rows - $i + 1; each cell prints the next $k.

2. Quick check. The last row is a single formatted 6.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset $k

Reset $k = 1 each row

That restarts the sequence every line. Keep $k outside both loops.

wrong bound

Loop $j down to 1 instead of $i

Every row would print the same length. Stop at $j >= $i.

no format

Use echo $k++ without width

Columns drift once values hit 10+. Prefer printf("%3d", $k++).

rows = 1

Single 1

Output is just the formatted 1.

rows ≤ 0

Empty output

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

Bad input

(int) alone is not enough

Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.

Time and Space Complexity

ProgramTimeExtra space
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Total printed values are n + (n-1) + … + 1 = n(n+1)/2, which is quadratic in n.

Key Takeaways

  • Rule: print continuous $k++; row $i has length $rows - $i + 1.
  • Inner bound: for ($j = $rows; $j >= $i; $j--) shrinks the row each time.
  • printf vs PHP_EOL: numbers stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 39 prints a rotating number pattern (12345, 23451, …).

One line: for each row, print the next values from a shared counter while the row length shrinks by one.

Frequently Asked Questions

Continuous numbers starting from 1, but each row has one fewer number: 5 on row 1, then 4, 3, 2, and 1 — totaling 15 numbers for 5 rows.
Counter $k starts at 1 before the loops and increments with $k++ each time a number prints — it is never reset inside the outer loop.
Row i prints rows - i + 1 numbers — the inner loop runs from $j = $rows down to $i.
The format specifier reserves 3 columns per number, keeping columns aligned when values become two digits.
Program 35 is right-aligned with leading spaces. Program 38 is left-aligned with decreasing row width and the same continuous counter.
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 trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
Only one row prints — a single number 1.

Did you know?

A counter $k starts at 1 and increments every time a number prints. Row $i prints $rows - $i + 1 numbers with printf("%3d", $k++) — total prints = n(n+1)/2.

Next: Rotating Number Pattern

Rotate the digit sequence on each row (12345, 23451, 34512…).

Program 39 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.

6 people found this page helpful