Reverse Growing Number Pattern in PHP

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Inner loop j=rows…i

What You’ll Learn

The Reverse Growing Number Pattern prints 5, 54, 543, 5432, 54321 — each row prints digits from $rows down to $i. This tutorial covers nested-loop logic, live preview, worked PHP examples, edge cases, and O(n²) complexity.

Shape Rule

$rows..$i per row

Each row prints $j from $rows down to $i with no spaces (e.g. when $i = 3543).

Nested Loops

$i = $rows..1

Outer loop lowers $i; inner loop runs $j = $rows..$i.

echo $j

No spaces

echo $j concatenates digits on the same row.

Foundation

Series base

Stepping stone toward Floyd’s triangle, stars, and alphabet patterns.

Live Preview

1–20 rows

Pick a row count and draw the pattern instantly in the browser.

O(n²)

Complexity

Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).

Introduction

A reverse growing number pattern grows each row by one digit on the right: 5, 54, 543, through 54321 for five rows. Row with index $i prints digits from $rows down to $i.

In PHP the outer loop runs $i = $rows..1, the inner loop prints $j from $rows down to $i, then echo PHP_EOL moves to the next line.

Why it matters?

It is a foundational nested-loop exercise — compare with Program 7 (growing reverse triangle) and continue to Program 9 (repeated number triangle).

Key Highlights

Counts down

Each row starts at $rows and stops at $i.

Growing rows

Inner loop $j = $rows..$i lengthens each row.

vs Program 4

Program 4 shrinks rows (54321, 5432); Program 8 grows rows (5, 54, 543) with the same inner $j = $rows..$i.

Series Foundation

Follow Program 4; continue to Program 6 (Reverse Growing) next.

In short: for each $i from $rows down to 1, print $j from $rows down to $i, then echo PHP_EOL.

📝 Problem & Approach

Given a positive integer $rows (e.g. 5), print a reverse growing number pattern: row $i prints digits $rows down to $i with no spaces (e.g. when $i = 3543).

PHP
// $rows = 5 (conceptual shape)
for ($i = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $j--) {
        echo $j;          // digits $rows..$i
    }
    echo PHP_EOL;         // next row
}

Inputs & Outputs

ItemTypeDescription
$rowsintNumber of triangle lines — outer loop runs from $rows down to 1.
$iintOuter loop — current row index; sets where the inner loop stops.
$jintInner loop — descending from $rows down to $i.

Minimal workflow

Pseudocode
for i from rows down to 1:
    for j from rows down to i:
        print j
    print newline

Approach comparison

ApproachIdeaBest for
Nested loops5, 54, 543, …Learning and interviews
User-input rowsfgets(STDIN);Flexible console programs
Spaced outputecho $j . " "Easier reading per row

⚡ Quick Reference

GoalPattern
Walk rowsfor ($i = $rows; $i >= 1; $i--)
Print digits $rows..$ifor ($j = $rows; $j >= $i; $j--) echo $j;
End the rowecho PHP_EOL;
Spaced digitsecho $j . " ";
User inputfgets(STDIN);
Program 4 contrastProgram 4 shrinks the prefix (54321, 5432); Program 8 grows it (5, 54, 543) with $j = $rows..$i

📋 Outer Loop vs Inner Loop vs Combined

How descending outer $i and inner $j = $rows..$i work together.

Outer loop
for ($i = $rows; $i >= 1; $i--)

Lowers stop index $i each row — triangle height.

Inner loop
for ($j = $rows; $j >= $i; $j--)

Prints $rows - $i + 1 digits on each row.

Cell value
echo $j

Concatenate digits with no spaces — 123 not 1 2 3.

Learning tip
trace i=3

Dry-run when i=3: j=5,4,3 → prints 543.

Context

When This Pattern Shows Up

Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.

  1. After Program 4

    Natural follow-up after Program 7’s growing reverse triangle — each row adds one more digit on the right.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with fgets(STDIN) for a flexible row count.

  4. Gateway to variants

    Compare with Program 4 (shrinking rows), then continue to Program 9 (repeated triangle).

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Enter a row count and draw the reverse growing number pattern in the browser.

Try 5, 7, or 10. Larger values still work up to 20.

Live result
Press "Draw pattern".

Examples Gallery

Three complete PHP programs — fixed $rows = 5, fgets(STDIN) input, and a spaced-output variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows — inner loop prints $rows..$i on each line.

Example 1 — Fixed $rows = 5

Hard-coded row count — inner loop prints from $rows down to $i.

PHP
<?php
$rows = 5;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

How It Works

When $i = 3, the inner loop prints 5, 4, 3 — output 543. When $i = 1, output is 54321.

📈 User Input

Read the row count with fgets(STDIN) instead of hard-coding 5.

Example 2 — User Input Version

Read $rows with fgets(STDIN); same nested loops as Example 1.

PHP
<?php
echo "Enter the number of rows: ";
$input = trim(fgets(STDIN));
if (!is_numeric($input)) {
    echo "Invalid input." . PHP_EOL;
    exit(1);
}
$rows = (int) $input;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $j--) {
        echo $j;
    }
    echo PHP_EOL;
}

How It Works

Same nested-loop core as Example 1; only the source of $rows changes.

⚡ Formatting Variant

Add spaces between digits for easier reading.

Example 3 — Spaced Output

Print a space after each digit with echo $j . " ".

PHP
<?php
$rows = 5;

for ($i = $rows; $i >= 1; $i--) {
    for ($j = $rows; $j >= $i; $j--) {
        echo $j . " ";
    }
    echo PHP_EOL;
}

How It Works

Same $j = $rows..$i logic; only the output format adds spaces between digits.

🧠 How the Algorithm Prints Rows

1

Set up

echo is built in; use fgets(STDIN) when reading input. Set loop variables $i, $j with $rows = 5.

Setup
2

Outer loop walks rows

for ($i = $rows; $i >= 1; $i--) — each row adds one more digit.

Row
3

Inner loop (j)

for ($j = $rows; $j >= $i; $j--) — prints digits from $i down to 1.

Inner
4

New line

echo PHP_EOL ends the row after the inner loop finishes.

Break
=

reverse growing number pattern complete

Each row grows by one digit on the right — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — row $i = 3 (when $rows = 5)

Trace how lowering $i lengthens each row toward 54321.

Row $i$j rangeOutput
555
45, 454
35, 4, 3543
25 … 25432
15 … 154321

After the inner loop, echo PHP_EOL moves to the next row.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: compare inner bounds with Program 4 and Program 7 and watch digit order change.

2. Pattern Series Base

Foundation for inverted, pyramid, diamond, and hollow variants.

Example: continue to Program 6 for the Reverse Growing pattern.

3. Console Formatting Drills

Practice echo vs echo PHP_EOL without complex math.

Example: put echo PHP_EOL inside the inner loop by mistake.

4. Spaced Output

Add spaces between digits once the two-loop structure works.

Example: use echo $j . " " between digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for $rows = 5 — total is still 15 (1+2+3+4+5)).

6. Input Validation Labs

Pair the pattern with fgets(STDIN) return checks and positive-row checks.

Example: reject $rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner PHP courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace $i and $j on paper for $rows = 3 before coding — watch how each row grows by one digit.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Match Inner Bounds

    Outer loop counts down; inner loop must run $j = $rows..$i so each row grows on the right.

  2. 2. Prefer fgets(STDIN)

    Call is_numeric($input) so bad input does not leave $rows uninitialized.

  3. 3. Keep echo PHP_EOL Outside

    Only call echo PHP_EOL after the inner loop finishes the row.

  4. 4. Use echo $j for Digits

    for ($j = $rows; $j >= $i; $j--) echo $j concatenates digits on one line.

  5. 5. Dry-Run $rows = 3

    Trace $i = 5, 4, 3 on paper before coding the full $rows = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put echo PHP_EOL inside the inner loop.

Common Pitfalls

Mistakes that commonly break Reverse Growing Number Pattern patterns.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use echo $j for digits; echo PHP_EOL only after the inner loop.

  2. 2. Ascending outer loop (Program 7)

    for ($i = 1; $i <= $rows; $i++) with $j = $i..1 prints 1, 21, 321 — not 5, 54, 543.

    → Use for ($i = $rows; $i >= 1; $i--) and for ($j = $rows; $j >= $i; $j--).

  3. 3. Program 7 inner loop

    for ($j = $i; $j >= 1; $j--) builds 1, 21, 321 — not this reverse-growing pattern.

    → Use for ($j = $rows; $j >= $i; $j--) so each row starts at $rows.

  4. 4. Confused with Program 4 shrinking rows

    Program 4 prints the longest row first (54321). Program 8 prints the shortest row first (5) with the same $j = $rows..$i range.

    → Check output order: growing rows need $i counting down from $rows.

  5. 5. Unchecked fgets(STDIN) input

    Letters or empty input leave $rows uninitialized.

    → Call is_numeric($input) and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single digit row

Output is just 1 on one line.

rows = 0

Empty pattern

Outer loop never runs — print nothing or show a message.

Negative

$rows < 0

Treat as invalid; re-prompt instead of silent empty output.

rows = 2

Smallest triangle

Two rows: 1 and 21.

Bad input

Non-numeric fgets(STDIN) input

Unchecked fgets(STDIN) input leaves $rows unset — call is_numeric($input) first.

Large rows

Large row count

Each row prints $i digits — total work grows as n(n+1)/2.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Compare Program 4

  • Program 4: fixed prefix 54321, 5432…
  • Review Program 4

2. Invert outer loop

  • Use for ($i = $rows; $i >= 1; $i--)
  • Same inner loop — tallest row first

3. Next in series

  • Continue with Program 9
  • Reverse growing pattern 5, 54, 543…

4. Spaced output

  • Use echo $j . " " between digits
  • Same loops, wider visual spacing

Notes

  • Descending inner loop. Outer loop: $i = $rows..1. Inner loop: $j = $rows..$i with echo $j.
  • echo stays on the line; echo PHP_EOL advances — mix them carefully.
  • Validate $rows > 0 for interactive programs; $rows = 1 should print a single 1.
  • Row $i prints $rows - $i + 1 digits ($rows..$i) — Program 4 uses the same inner range but shrinks row by row instead of growing.

Quick Takeaway: outer loop $i = $rows..1, inner loop $j = $rows..$i with echo $j, then echo PHP_EOL.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Spaced output (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The Reverse Growing Number Pattern is a compact nested-loop lesson: outer loop lowers $i while the inner loop prints digits $rows down to $i. Master the fixed-$rows version, then try user input and spaced output.

Practice the three examples above, then continue to Program 9 for the repeated number triangle (1, 22, 333, 4444, 55555).

Each row prints $rows..$i — keep echo $j for digits and echo PHP_EOL for the row break.

💡 Best Practices

✅ Do

  • Use for ($i = $rows; $i >= 1; $i--) in the outer loop
  • Inner: for ($j = $rows; $j >= $i; $j--) prints digits i..1
  • Use echo $j for digits and echo PHP_EOL after each row
  • Validate $rows ≥ 1 for interactive programs
  • Call is_numeric($input) before using $rows

❌ Don’t

  • Call echo PHP_EOL inside the inner digit loop
  • Use Program 7’s ascending outer loop when you meant Program 8 — this pattern needs j-- from $i
  • Forget echo PHP_EOL inside the inner loop — digits must stay on one row
  • Ignore bad console input in user-facing demos
  • Skip the $rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this reverse growing number pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

Counts down $i

Code
03

Inner loop

$j = $rows down to $i

Code
04

Newline

Ends each row

Shape
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

When $i = $rows, the inner loop runs $j from $rows down to $i ($rows), so only one digit prints. Each later row lowers $i, so more digits appear: 54, 543, and so on.
The outer loop runs $i from $rows down to 1. For each $i, the inner loop runs $j from $rows down to $i and echoes $j, then echo PHP_EOL ends the row.
Program 4 shrinks each row from $rows down to $i (54321, 5432, 543). Program 8 grows each row from one digit up to $rows digits (5, 54, 543, 5432, 54321) using the same inner range $j = $rows..$i.
Program 7 uses outer $i = 1..$rows and inner $j = $i..1 (1, 21, 321). Program 8 uses outer $i = $rows..1 and inner $j = $rows..$i (5, 54, 543).
Yes. Use echo $j . " " inside the inner loop — see Example 3.
O(n²) for n rows. Total printed digits are 1+2+...+n = n(n+1)/2.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you Know? 🔊

Outer loop counts $i down from $rows; inner loop prints $j from $rows down to $i5, 54, 543, … O(n²) for $n rows.

Continue to Program 9

Move on to the repeated number triangle pattern in the PHP number-pattern series.

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