PHP Powers of 11 Number Pattern

Definition
What Is This Pattern?
The powers of 11 pattern prints one number per line, starting at 1 and multiplying by 11 each row.
Rule: res starts at 1; each next row is res * 11
1
11
121
1331
14641 ← n = 5
In PHP one loop is enough: keep a running $res, print it, then $res *= 11. No nested loops required.
Approach
How to Solve It
One loop, one running variable — print, then multiply by 11.
| Method | Idea | Best for |
|---|
| Print then multiply | echo $res . PHP_EOL; $res *= 11; | Cleanest loop body |
| If on first row | if ($i == 1) $res = 1; else $res *= 11; | Matching older textbook code |
Pseudocode
res = 1
for i from 1 to n:
print res
res = res * 11
Cheat sheet
| Goal | Pattern |
|---|
| Start value | $res = 1; |
| Loop rows | for ($i = 1; $i <= $n; $i++) |
| Print line | echo $res . PHP_EOL; |
| Next term | $res *= 11; |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
echo $res . PHP_EOL | Prints the value and ends the line | Preferred — one call per row |
echo $res + echo PHP_EOL | Same result in two calls | Older textbook style (Example 1) |
Same idea as C# WriteLine(res): one value per line. Prefer a single echo $res . PHP_EOL over split print + newline.
Try it
Live Preview
Change the row count and the powers-of-11 sequence updates instantly.
Trace
Worked Walkthrough — $n = 5
Trace $res before and after each multiply.
| Row | Print | Then $res *= 11 |
|---|
1 | 1 | 11 |
2 | 11 | 121 |
3 | 121 | 1331 |
4 | 1331 | 14641 |
5 | 14641 | 161051 (not printed) |
Total prints = $n. Values grow fast — keep $n small while learning.
Code
PHP Programs
Three complete programs: fixed 5 rows with an if-check, user-input rows, and a clean print-then-multiply loop. Use View Output for sample results.
Example 1 — Fixed $n = 5
Textbook style with a special case on the first row.
<?php
$res = 1;
for ($i = 1; $i <= 5; $i++) {
if ($i == 1)
$res = $i;
else
$res = $res * 11;
echo $res;
echo PHP_EOL;
}
How It Works
1. First row. When $i == 1, set $res = 1 and print it.
2. Later rows. Multiply the previous $res by 11, then print.
3. New line. echo $res + echo PHP_EOL ends each row (or use echo $res . PHP_EOL).
Example 2 — User Input (rows)
Read $n safely and print the first $n terms.
<?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);
}
$n = (int) $input;
$res = 1;
for ($i = 1; $i <= $n; $i++) {
if ($i == 1)
$res = 1;
else
$res = $res * 11;
echo $res . PHP_EOL;
}
How It Works
1. Validate n. Check is_numeric; require $n >= 1.
2. Same sequence. Only the loop bound changes from the literal 5.
Example 3 — Print Then Multiply
No special-case if — print first, then update.
<?php
$res = 1;
for ($i = 1; $i <= 5; $i++) {
echo $res . PHP_EOL;
$res *= 11;
}
How It Works
1. Print current. Start with $res = 1; each iteration prints the current value.
2. Advance. $res *= 11 prepares the next row — no if needed.
Edge Cases & Pitfalls
Check these before calling the solution done.
orderMultiply before the first print
If you do $res *= 11 before printing when $res starts at 1, the first line becomes 11. Print first.
growthValues grow very fast
11^n balloon quickly. Start with small $n (like 5) while learning.
* 10Multiply by 10 by mistake
You get 1, 10, 100, 1000… Keep the factor 11.
n = 1Single row
Output is just 1 — valid and useful for testing.
PascalExpecting Pascal digits forever
After 14641, carries appear (161051). Digits no longer match binomial coefficients.
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 / clean (Examples 1, 3) | O($n) | O(1) |
| User input (Example 2) | O($n) | O(1) |
One multiply and one print per row — linear in the number of rows (unlike nested-loop patterns).
Remember
Key Takeaways
Rule: start at 1; each next line multiplies the previous value by 11.
Clean loop: echo $res . PHP_EOL; $res *= 11; — no if needed.
echo + PHP_EOL: one value per row — prefer echo $res . PHP_EOL.
Next step: Program 49 prints a triangular multiplication pattern ($i*$j).
One line: print the current power-of-11 term, then multiply by 11 for the next row.
Frequently Asked Questions
It starts with $res = 1 and multiplies $res by 11 for each next row. That produces 1, 11, 121, 1331, 14641 for the first five lines.
Yes — increase the loop limit or read $n from input. For very large n, values grow fast; PHP keeps integers as long as they fit platform limits.
11^n shows binomial digits only while there are no base-10 carries. Once carries occur, digits no longer match the triangle.
O(n) for n rows because the program computes and prints one value per row.
Program 47 prints a 2D concentric diamond with nested loops. Program 48 prints a 1D growing sequence with one loop.
Yes — print first, then multiply: echo $res . PHP_EOL; $res *= 11; — see Example 3.
161051 — still valid, but digit carries mean it no longer mirrors Pascal row coefficients.
No — a single loop with a running variable is enough for this sequence.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know?
Start with $res = 1, print it, then update with $res *= 11 each row. The first five lines are 1, 11, 121, 1331, 14641 — one value per line, O(n) time.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
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