Shape Rule
× 11 each row
Start $res = 1; each iteration prints $res, then $res *= 11.

The powers-of-11 pattern prints 1, 11, 121, 1331, 14641 — each line is the previous value multiplied by 11. This tutorial covers the single-loop logic, overflow-safe BCMath variant, live preview, worked PHP examples, edge cases, and complexity.
× 11 each row
Start $res = 1; each iteration prints $res, then $res *= 11.
i = 1..rows
for ($i = 1; $i <= $rows; $i++) — one line printed per iteration.
BCMath
Use int for small demos; switch to bcmul() when rows grow — Example 2.
Early rows
First few lines match Pascal’s triangle rows written without spaces.
3–12 rows
Pick a row count and draw the powers of 11 pattern instantly in the browser.
Complexity
One loop iteration per row — linear time; extra memory stays O(1).
A powers-of-11 number pattern prints 1, then 11, then 121, up to 14641 for five rows. Each line equals the previous value times 11.
In PHP you initialize $res = 1, loop $rows times, call echo $res . PHP_EOL, then update with $res *= 11.
It is a compact single-loop exercise that also connects to Pascal’s triangle and overflow awareness.
$res = 1 produces the first line.
$res *= 11 after each print.
No nested loops — one iteration, one line.
Follow Program 47 concentric diamond; continue to Program 49 multiplication triangle.
In short: $res = 1, loop $rows times, echo $res . PHP_EOL, then $res *= 11.
Given $rows = 5, print five lines: 1, 11, 121, 1331, 14641.
// $rows = 5 (conceptual output)
// 1
// 11
// 121
// 1331
// 14641 | Item | Type | Description |
|---|---|---|
$rows | int | How many lines to print (typically ≥ 1). |
$res | int / BCMath | Running value — starts at 1, multiplied by 11 each step. |
| Printed output | text | One number per line — $rows total lines. |
$res = 1
for $i from 1 to $rows:
print $res
$res = $res * 11 | Approach | Idea | Best for |
|---|---|---|
| int multiply | $res = $res * 11 after each print | Small row counts (≤ ~9 safely) |
| BCMath + fgets(STDIN) | bcmul($res, '11') | Large row counts without overflow |
| Single-line output | echo $res . " " | Compact one-row display — Example 3 |
| Goal | Pattern |
|---|---|
| Initialize | $res = 1; |
| Loop rows | for ($i = 1; $i <= $rows; $i++) |
| Print line | echo $res . PHP_EOL; |
| Update | $res = $res * 11; |
| BCMath update | $res = bcmul($res, '11'); |
| Program 47 contrast | Concentric diamond uses nested loops; this pattern uses one loop and multiply-by-11 |
Three phases of each loop iteration — print the current value, then prepare the next line.
$res = 1First line is always 1 before any multiplication.
echo $res . PHP_EOLOutput the current value on its own line.
$res *= 11Multiply by 11 to get the next row’s value.
trace $i=3Dry-run iteration 3: $res=121 → prints 121, then $res=1331.
Reach for this pattern when teaching single-loop series, overflow awareness, and Pascal’s-triangle connections.
Classic follow-up after concentric diamonds and single-loop series patterns.
Introduce int vs bcmul() when values grow quickly.
Combine loops with fgets(STDIN) for a flexible row count.
Compare with Program 47 (concentric diamond), then continue to Program 49 (multiplication triangle).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in loop variables, running totals, and O(n) thinking.
Choose a row count and draw the powers of 11 pattern in the browser.
Three complete PHP programs — fixed $rows with int, bcmul() + fgets(STDIN), and a single-line output variant. Click View Output to reveal sample console results.
Print five lines with a single loop and multiply-by-11 updates.
$rows = 5 (int)Hard-coded size — print $res, then multiply by 11 each iteration.
<?php
$rows = 5;
$res = 1;
for ($i = 1; $i <= $rows; $i++) {
echo $res . PHP_EOL;
$res *= 11;
} Iteration 1 prints 1, then $res becomes 11. Iteration 2 prints 11, then $res becomes 121 — and so on.
Use bcmul() so larger row counts do not overflow.
Read $rows with fgets(STDIN) and multiply with bcmul().
<?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;
$res = '1';
for ($i = 1; $i <= $rows; $i++) {
echo $res . PHP_EOL;
$res = bcmul($res, '11');
} Same loop structure as Example 1; bcmul() grows without the overflow limits of int.
Print all values on one line separated by spaces.
Use echo with a trailing space, then one final PHP_EOL.
<?php
$rows = 5;
$res = 1;
for ($i = 1; $i <= $rows; $i++) {
echo $res . " ";
$res *= 11;
}
echo PHP_EOL; Same multiply-by-11 logic; only the output format changes — one horizontal line instead of five vertical lines.
echo is built in; use fgets(STDIN) when reading input. Set $rows and initialize $res = 1.
for ($i = 1; $i <= $rows; $i++) — one iteration per output line.
echo $res . PHP_EOL then $res = $res * 11 prepares the next line.
After 5 iterations: 1, 11, 121, 1331, 14641 — linear O(n) work.
Total lines printed = rows — O(n) time, O(1) extra memory.
i = 3Trace the third loop iteration to see print-then-multiply in action.
| Step | res before | Action |
|---|---|---|
| i = 1 | 1 | print 1 → res = 11 |
| i = 2 | 11 | print 11 → res = 121 |
| i = 3 | 121 | print 121 → res = 1331 |
Line 3 output: 121 — five rows produce five values ending at 14641.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Classic intro to accumulator variables updated each iteration.
Example: use bcmul() for large row counts — see Example 2.
Early rows match Pascal without spaces — great math connection.
Example: compare row 5 (14641) with Pascal row coefficients.
Practice PHP_EOL vs print for multi-line vs single-line output.
Example: put echo $res . " " for one-line output — Example 3.
Watch int and int limits as values grow by 11 each step.
Example: print 10+ rows and observe when int wraps.
One loop iteration per row makes O(n) concrete for beginners.
Example: count lines for rows=5 → five values from 1 up to 14641.
Pair the pattern with fgets(STDIN) and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the print-then-update order first — then write the loop. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner PHP courses.
Wrong update order (multiply before print) skips the first line 1.
Only loops and console output — no arrays or math libraries.
Change rows, switch to BCMath, or print on one line with spaces.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the fixed-$rows loop first; then try BCMath input and the single-line variant in Example 3.
Small habits that keep number-pattern code clean.
Use $rows for the loop bound and $res for the running value.
fgets(STDIN)Avoid crashes when the user types letters instead of a number.
Always echo $res . PHP_EOL before $res *= 11 so the first line is 1.
Use int for demos; switch to bcmul() when rows grow.
Trace $rows = 3 on paper before coding larger demos.
Pro Tip: if the first line is missing or wrong, check whether you multiply before printing.
Mistakes that commonly break powers of 11 number patterns.
Updating $res first skips the initial value 1 on line one.
→ Print $res, then multiply: $res = $res * 11.
int overflows after a few multiplications by 11 — values become negative or wrong.
→ Use int for small demos or bcmul() for larger row counts.
Wrong initial value shifts the entire sequence.
→ Initialize $res = 1 (or BCMath.ONE).
Using only echo with spaces may leave the cursor on the same line as the last value.
→ Add echo PHP_EOL after the loop — Example 3.
Letters or empty input leave $rows unset.
→ Use is_numeric($input) and re-prompt on failure.
Using literal 5 in loop bounds instead of variable $rows breaks dynamic input.
→ Use one $rows variable for the loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
int overflows around row 10; use bcmul() for more lines.
Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.
Use echo $res . " ") for one horizontal line — see Example 3.
Try these variations to lock in the pattern.
$rows = 3, 6, or 8int overflows$rows (e.g. 5 lines for rows=5).print stays on the line; PHP_EOL advances — mix them carefully.$rows > 0 for interactive programs; $rows = 1 prints one value.1.Quick Takeaway: set $res=1, loop $rows times, echo $res . PHP_EOL, then $res *= 11.
| Program | Time | Extra space |
|---|---|---|
| Fixed rows (Example 1) | O(n) | O(1) |
| BCMath + fgets(STDIN) (Example 2) | O(n) loop; multiply cost grows with digits | O(1) |
| Single-line output (Example 3) | O(n) | O(1) |
The powers of 11 pattern combines a single loop with repeated multiplication — a natural step after concentric number diamonds. Master the fixed-$rows version first, then try BCMath input and the single-line variant in Example 3.
Practice the three examples above, then continue to Program 49 for the multiplication number triangle pattern.
Print before update — keep $res = 1 as the starting value.
$res=1, print, and $res*=11 before codingecho $res . PHP_EOL then $res *= 11 each iterationrows ≥ 1 for interactive programsfgets(STDIN) return value before using $rows1)int for many rows (overflows quickly)$rows$rows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints $res
res = 1
Code$res *= 11
Logicn iterations
O(n)
AnalysisStart with $res = 1, print it, then multiply by 11 each row. The first five lines are 1, 11, 121, 1331, 14641 — early rows resemble Pascal’s triangle without spaces.
Move on to the multiplication number triangle pattern in the PHP number-pattern series.
12 people found this page helpful