PHP Multiplication Number Triangle Pattern

Definition
What Is This Pattern?
A triangular multiplication pattern prints the products $i × 1, $i × 2, …, $i × $i on row $i.
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.
Approach
How to Solve It
One nested loop: row multiplier outside, column factors inside.
| Method | Idea | Best for |
|---|
| Product triangle | Print $i * $j for $j = 1..$i | Learning, interviews, exams |
| Fixed width | Same products with printf("%3d", ...) | Neater console output |
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print i * j and a space
print newline
Cheat sheet
| Goal | Pattern |
|---|
| Walk rows | for ($i = 1; $i <= $rows; $i++) |
| Walk columns | for ($j = 1; $j <= $i; $j++) |
| Print product | echo ($i * $j) . " "; |
| Aligned columns | printf("%3d", $i * $j); |
| End the row | echo PHP_EOL; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo ($i * $j) . " " | Stays on the same line | Each product (plus space or width) |
echo PHP_EOL | Ends the current line | After the inner loop finishes a row |
Print products without a newline, then end the row once.
Try it
Live Preview
Change the row count and the multiplication triangle updates instantly.
Trace
Worked Walkthrough — $rows = 4
Trace each row’s products from $i*1 through $i*$i.
$i | Products $i*$j | Printed row |
|---|
1 | 1 | 1 |
2 | 2, 4 | 2 4 |
3 | 3, 6, 9 | 3 6 9 |
4 | 4, 8, 12, 16 | 4 8 12 16 |
Total products for $n rows = $n($n+1)/2. Last value on row $i is always $i*$i.
Code
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
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++)
echo ($i * $j) . " ";
echo PHP_EOL;
}
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
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
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
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++)
printf("%3d", $i * $j);
echo PHP_EOL;
}
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
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 <= rowsInner loop always to $rows
That prints a full rectangle (multiplication table). Keep $j <= $i for the triangle.
j * iSwap to $j * $i only
Same products — fine. Just stay consistent; both equal $i * $j.
PHP_EOLecho PHP_EOL inside the inner loop
That puts every product on its own line. Call echo PHP_EOL only after the column loop.
rows = 1Single row
Output is just 1 — one product on one line.
no spacePrint products without a separator
Digits run together (24812). Keep a space or fixed width between values.
Bad inputCasting without checks
Prefer is_numeric after trim(fgets(STDIN)) so non-numeric input does not silently become 0.
Analysis
Time and Space Complexity
| Program | Time | Extra 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.
Remember
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
Developer, cloud engineer, and technical writer
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