Cumulative Step Number Triangle in PHP

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Variables

What You’ll Learn

How to print a cumulative-step number triangle in PHP using nested loops and two helper variables.

You’ll see how dynamic increments can produce non-linear row sequences.

⭐ Pattern Output

For 5 rows, the pattern looks like this:

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

Complete PHP Program

Start each row at $i, then increase by a decreasing step value to generate subsequent terms.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    $k = 4;
    $res = $i;
    for ($j = $i; $j < $i + $i; $j++) {
        if ($i == $j) {
            echo $j . " ";
        } else {
            $res = $res + $k;
            echo $res . " ";
            $k--;
        }
    }
    echo PHP_EOL;
}

🧠 How It Works

1

Outer loop sets row

for ($i = 1; $i <= 5; $i++) controls row count and first value.

Row control
2

Initialize helpers

$k = 4 is step size, $res = $i stores latest printed value.

Setup
3

Generate row values

First value is $i. Next values use $res += $k and then $k--.

Sequence rule
4

Next line

echo PHP_EOL; prints each row on a new line.

Formatting
=

Step-based triangle rows

Rows grow from 1 to n terms, so total printed values are triangular, giving O(n²).

2

Variation — User Input (CLI) Version

This version accepts row count from user input (run from terminal with php):

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

if ($n < 1) {
    echo "Rows must be at least 1" . PHP_EOL;
    exit;
}

for ($i = 1; $i <= $n; $i++) {
    $k = $n - 1;
    $res = $i;
    for ($j = $i; $j < $i + $i; $j++) {
        if ($i == $j) {
            echo $j . " ";
        } else {
            $res += $k;
            echo $res . " ";
            $k--;
        }
    }
    echo PHP_EOL;
}

💡 Tips for Enhancement

Try These

  • Add spacing alignment with str_pad()
  • Generalize step initialization for different row sizes
  • Reverse sequence direction for a mirrored variant
  • Store rows in arrays for reuse/testing
  • Use custom starting value instead of row index

Avoid

  • Using HTML wrappers in CLI-oriented pattern logic
  • Forgetting to decrement step $k after each addition
  • Skipping input validation for row size
  • Changing inner loop bounds without updating formula logic

Key Takeaways

1

Each row starts at i and adds decreasing steps.

2

Variables $res and $k manage sequence generation.

3

Inner loop prints exactly i values per row.

4

This demonstrates state-based pattern creation, not simple arithmetic progression.

❓ Frequently Asked Questions

Start at 4, then add steps 4, 3, 2 successively to get 8, 11, 13.
Yes. Set $n = 8 and initialize step as $n - 1 in dynamic version.
The decreasing step creates the intended row shape; constant steps would produce a different pattern.
O(n²) due to triangular total of printed values.

Explore More PHP Number Patterns!

Practice variable-driven sequence building with more advanced pattern programs.

All Number Patterns →
Did you know?

Patterns like this are useful for understanding how state variables evolve across nested loops, a key concept for many dynamic programming tasks.

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