PHP Multiplication Number Triangle Pattern

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

What Is This Pattern?

A triangular multiplication pattern prints the products $i × 1, $i × 2, …, $i × $i on row $i.

Remember
Rule: on row i, print i*j for j = 1..i

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25   ← rows = 5

In PHP the outer loop sets the row multiplier $i; the inner loop runs $j from 1 to $i and prints $i * $j.

How to Solve It

One nested loop: row multiplier outside, column factors inside.

MethodIdeaBest for
Product trianglePrint $i * $j for $j = 1..$iLearning, interviews, exams
Fixed widthSame products with printf("%3d", ...)Neater console output

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i * j and a space
    print newline

Cheat sheet

GoalPattern
Walk rowsfor ($i = 1; $i <= $rows; $i++)
Walk columnsfor ($j = 1; $j <= $i; $j++)
Print productecho ($i * $j) . " ";
Aligned columnsprintf("%3d", $i * $j);
End the rowecho PHP_EOL;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo ($i * $j) . " "Stays on the same lineEach product (plus space or width)
echo PHP_EOLEnds the current lineAfter the inner loop finishes a row

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

Live Preview

Change the row count and the multiplication triangle updates instantly.

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

Live result 5 rows · 15 products
1 
2 4 
3 6 9 
4 8 12 16 
5 10 15 20 25 

Worked Walkthrough — $rows = 4

Trace each row’s products from $i*1 through $i*$i.

$iProducts $i*$jPrinted row
111
22, 42 4
33, 6, 93 6 9
44, 8, 12, 164 8 12 16

Total products for $n rows = $n($n+1)/2. Last value on row $i is always $i*$i.

PHP Programs

Three complete programs: fixed 5 rows, user-input rows, and fixed-width columns. Use View Output for sample results.

Example 1 — Fixed $rows = 5

Nested loops print $i * $j with a trailing space.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++)
        echo ($i * $j) . " ";

    echo PHP_EOL;
}

How It Works

1. Outer loop. $i is the row multiplier — row 3 uses 3 as the base.

2. Inner loop. $j runs from 1 to $i; each cell prints $i * $j.

3. New line. echo PHP_EOL after the inner loop starts the next row.

Example 2 — User Input (rows)

Read the row count and build the same triangle.

PHP
<?php
echo "Enter number of 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;

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

    echo PHP_EOL;
}

How It Works

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

2. Same triangle. Only the outer bound changes from the literal 5.

Example 3 — Fixed-Width Columns

Use printf("%3d", ...) so single- and double-digit products stay aligned.

PHP
<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++)
        printf("%3d", $i * $j);

    echo PHP_EOL;
}

How It Works

1. Same products. Still $i * $j for $j = 1..$i.

2. Alignment. Width 3 pads each value so columns line up when digits grow.

Edge Cases & Pitfalls

Check these before calling the solution done.

j <= rows

Inner loop always to $rows

That prints a full rectangle (multiplication table). Keep $j <= $i for the triangle.

j * i

Swap to $j * $i only

Same products — fine. Just stay consistent; both equal $i * $j.

PHP_EOL

echo PHP_EOL inside the inner loop

That puts every product on its own line. Call echo PHP_EOL only after the column loop.

rows = 1

Single row

Output is just 1 — one product on one line.

no space

Print products without a separator

Digits run together (24812). Keep a space or fixed width between values.

Bad input

Casting without checks

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

Time and Space Complexity

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

Total products are 1 + 2 + … + $n = $n($n+1)/2, so work is quadratic in the row count.

Key Takeaways

  • Rule: on row $i, print $i*$j for $j = 1..$i; last value is $i*$i.
  • Shape: triangular — row length grows with $i; total prints = $n($n+1)/2.
  • echo vs PHP_EOL: products stay on the line; echo PHP_EOL advances after each row.
  • Next step: Program 50 prints a mixed descending/ascending number triangle.

One line: for each row $i, print the first $i multiples of $i.

Frequently Asked Questions

A triangle where row i contains i values: i*1, i*2, … up to i*i. Row 4 prints 4 8 12 16.
Outer loop sets row $i. Inner loop runs $j = 1..$i and prints $i*$j for each column.
The last value on row $i is $i*$i. For $i = 5, that is 25.
Yes — change the loop limit or read rows from user input with fgets and is_numeric — see Example 2.
Change the inner loop to $j = 1..$rows on every row: for ($j = 1; $j <= $rows; $j++).
O(n²) for n rows because total prints are 1+2+…+n = n(n+1)/2.
Program 48 is a 1D powers-of-11 sequence with one loop. Program 49 uses nested loops for a multiplication triangle.
Use fixed-width formatting like printf("%3d", $i * $j) so columns line up — see Example 3.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.

Did you know?

On row $i, print $i products: $i*1, $i*2, … up to $i*$i. Total prints = n(n+1)/2 — a triangular number, so time is O(n²).

Next: Mixed Number Triangle

Print rows that descend then ascend (12345, 21234, 32123…).

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