PHP Powers of 11 Number Pattern

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

What Is This Pattern?

The powers of 11 pattern prints one number per line, starting at 1 and multiplying by 11 each row.

Remember
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.

How to Solve It

One loop, one running variable — print, then multiply by 11.

MethodIdeaBest for
Print then multiplyecho $res . PHP_EOL; $res *= 11;Cleanest loop body
If on first rowif ($i == 1) $res = 1; else $res *= 11;Matching older textbook code

Pseudocode

Pseudocode
res = 1
for i from 1 to n:
    print res
    res = res * 11

Cheat sheet

GoalPattern
Start value$res = 1;
Loop rowsfor ($i = 1; $i <= $n; $i++)
Print lineecho $res . PHP_EOL;
Next term$res *= 11;

Printing Numbers vs Starting a New Line

APIEffectUse for
echo $res . PHP_EOLPrints the value and ends the linePreferred — one call per row
echo $res + echo PHP_EOLSame result in two callsOlder 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.

Live Preview

Change the row count and the powers-of-11 sequence updates instantly.

Whole numbers from 1 to 8 (keeps values within a comfortable range). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · last = 14641
1
11
121
1331
14641

Worked Walkthrough — $n = 5

Trace $res before and after each multiply.

RowPrintThen $res *= 11
1111
211121
31211331
4133114641
514641161051 (not printed)

Total prints = $n. Values grow fast — keep $n small while learning.

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
<?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
<?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
<?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.

order

Multiply before the first print

If you do $res *= 11 before printing when $res starts at 1, the first line becomes 11. Print first.

growth

Values grow very fast

11^n balloon quickly. Start with small $n (like 5) while learning.

* 10

Multiply by 10 by mistake

You get 1, 10, 100, 1000… Keep the factor 11.

n = 1

Single row

Output is just 1 — valid and useful for testing.

Pascal

Expecting Pascal digits forever

After 14641, carries appear (161051). Digits no longer match binomial coefficients.

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 / 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).

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.

Next: Multiplication Number Triangle

Print row products $i*1, $i*2, … up to $i*$i.

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