Powers of 11 Number Pattern in PHP

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Single Loop × 11

What You’ll Learn

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.

Shape Rule

× 11 each row

Start $res = 1; each iteration prints $res, then $res *= 11.

Single Loop

i = 1..rows

for ($i = 1; $i <= $rows; $i++) — one line printed per iteration.

Overflow Safe

BCMath

Use int for small demos; switch to bcmul() when rows grow — Example 2.

Pascal Link

Early rows

First few lines match Pascal’s triangle rows written without spaces.

Live Preview

3–12 rows

Pick a row count and draw the powers of 11 pattern instantly in the browser.

O(n)

Complexity

One loop iteration per row — linear time; extra memory stays O(1).

Introduction

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.

Why it matters?

It is a compact single-loop exercise that also connects to Pascal’s triangle and overflow awareness.

Key Highlights

Start at 1

$res = 1 produces the first line.

Multiply by 11

$res *= 11 after each print.

One Loop

No nested loops — one iteration, one line.

Series Foundation

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.

📝 Problem & Approach

Given $rows = 5, print five lines: 1, 11, 121, 1331, 14641.

PHP
// $rows = 5 (conceptual output)
// 1
// 11
// 121
// 1331
// 14641

Inputs & Outputs

ItemTypeDescription
$rowsintHow many lines to print (typically ≥ 1).
$resint / BCMathRunning value — starts at 1, multiplied by 11 each step.
Printed outputtextOne number per line — $rows total lines.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
int multiply$res = $res * 11 after each printSmall row counts (≤ ~9 safely)
BCMath + fgets(STDIN)bcmul($res, '11')Large row counts without overflow
Single-line outputecho $res . " "Compact one-row display — Example 3

⚡ Quick Reference

GoalPattern
Initialize$res = 1;
Loop rowsfor ($i = 1; $i <= $rows; $i++)
Print lineecho $res . PHP_EOL;
Update$res = $res * 11;
BCMath update$res = bcmul($res, '11');
Program 47 contrastConcentric diamond uses nested loops; this pattern uses one loop and multiply-by-11

📋 Print vs Update vs Combined

Three phases of each loop iteration — print the current value, then prepare the next line.

Initialize
$res = 1

First line is always 1 before any multiplication.

Print
echo $res . PHP_EOL

Output the current value on its own line.

Update
$res *= 11

Multiply by 11 to get the next row’s value.

Learning tip
trace $i=3

Dry-run iteration 3: $res=121 → prints 121, then $res=1331.

Context

When This Pattern Shows Up

Reach for this pattern when teaching single-loop series, overflow awareness, and Pascal’s-triangle connections.

  1. First lab exercise

    Classic follow-up after concentric diamonds and single-loop series patterns.

  2. Series & overflow lesson

    Introduce int vs bcmul() when values grow quickly.

  3. Console I/O practice

    Combine loops with fgets(STDIN) for a flexible row count.

  4. Gateway to variants

    Compare with Program 47 (concentric diamond), then continue to Program 49 (multiplication triangle).

  5. Not a UI layout tool

    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.

🔮 Live Preview

Choose a row count and draw the powers of 11 pattern in the browser.

Try 3, 5, or 8 rows (up to 12).

Live result
Press "Draw pattern".

Examples Gallery

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.

📚 Getting Started

Print five lines with a single loop and multiply-by-11 updates.

Example 1 — Fixed $rows = 5 (int)

Hard-coded size — print $res, then multiply by 11 each iteration.

PHP
<?php
$rows = 5;
$res = 1;

for ($i = 1; $i <= $rows; $i++) {
    echo $res . PHP_EOL;
    $res *= 11;
}

How It Works

Iteration 1 prints 1, then $res becomes 11. Iteration 2 prints 11, then $res becomes 121 — and so on.

📈 Practical Variant

Use bcmul() so larger row counts do not overflow.

Example 2 — BCMath + fgets(STDIN)

Read $rows with fgets(STDIN) and multiply with bcmul().

PHP
<?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');
}

How It Works

Same loop structure as Example 1; bcmul() grows without the overflow limits of int.

⚡ Readability Variant

Print all values on one line separated by spaces.

Example 3 — Single-Line Output

Use echo with a trailing space, then one final PHP_EOL.

PHP
<?php
$rows = 5;
$res = 1;

for ($i = 1; $i <= $rows; $i++) {
    echo $res . " ";
    $res *= 11;
}
echo PHP_EOL;

How It Works

Same multiply-by-11 logic; only the output format changes — one horizontal line instead of five vertical lines.

🧠 How the Algorithm Prints Rows

1

Set up

echo is built in; use fgets(STDIN) when reading input. Set $rows and initialize $res = 1.

Setup
2

Loop rows

for ($i = 1; $i <= $rows; $i++) — one iteration per output line.

Loop
3

Print then update

echo $res . PHP_EOL then $res = $res * 11 prepares the next line.

Update
4

Result

After 5 iterations: 1, 11, 121, 1331, 14641 — linear O(n) work.

Done
=

Powers of 11 number pattern complete

Total lines printed = rowsO(n) time, O(1) extra memory.

🔎 Worked Walkthrough — iteration i = 3

Trace the third loop iteration to see print-then-multiply in action.

Stepres beforeAction
i = 11print 1 → res = 11
i = 211print 11 → res = 121
i = 3121print 121 → res = 1331

Line 3 output: 121 — five rows produce five values ending at 14641.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Running Totals

Classic intro to accumulator variables updated each iteration.

Example: use bcmul() for large row counts — see Example 2.

2. Pascal’s Triangle Link

Early rows match Pascal without spaces — great math connection.

Example: compare row 5 (14641) with Pascal row coefficients.

3. Console Formatting Drills

Practice PHP_EOL vs print for multi-line vs single-line output.

Example: put echo $res . " " for one-line output — Example 3.

4. Overflow Awareness

Watch int and int limits as values grow by 11 each step.

Example: print 10+ rows and observe when int wraps.

5. Complexity Intuition

One loop iteration per row makes O(n) concrete for beginners.

Example: count lines for rows=5 → five values from 1 up to 14641.

6. Input Validation Labs

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.

Advantages

Why this pattern earns a permanent spot in beginner PHP courses.

  1. 1. Instant Visual Feedback

    Wrong update order (multiply before print) skips the first line 1.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Change rows, switch to BCMath, or print on one line with spaces.

  4. 4. O(1) Extra Memory

    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.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Variables Clearly

    Use $rows for the loop bound and $res for the running value.

  2. 2. Prefer fgets(STDIN)

    Avoid crashes when the user types letters instead of a number.

  3. 3. Print Before Update

    Always echo $res . PHP_EOL before $res *= 11 so the first line is 1.

  4. 4. Pick the Right Type

    Use int for demos; switch to bcmul() when rows grow.

  5. 5. Dry-Run One Small n

    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.

Common Pitfalls

Mistakes that commonly break powers of 11 number patterns.

  1. 1. Multiplying Before Printing

    Updating $res first skips the initial value 1 on line one.

    → Print $res, then multiply: $res = $res * 11.

  2. 2. Using int for Many Rows

    int overflows after a few multiplications by 11 — values become negative or wrong.

    → Use int for small demos or bcmul() for larger row counts.

  3. 3. Starting res at 0 or 11

    Wrong initial value shifts the entire sequence.

    → Initialize $res = 1 (or BCMath.ONE).

  4. 4. Forgetting Final Newline (Single-Line Variant)

    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.

  5. 5. Unchecked fgets(STDIN) Input

    Letters or empty input leave $rows unset.

    → Use is_numeric($input) and re-prompt on failure.

  6. 6. Hard-coding 5 Everywhere

    Using literal 5 in loop bounds instead of variable $rows breaks dynamic input.

    → Use one $rows variable for the loop bound.

Edge Cases

Check these inputs before calling the solution done.

$rows = 1

Single line

Output is one line: 1.

$rows = 0

Empty pattern

Loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

int overflows around row 10; use bcmul() for more lines.

Bad input

Non-numeric fgets(STDIN) input

Unchecked fgets(STDIN) leaves $rows unset — call is_numeric($input) first.

Compact

Single-line form

Use echo $res . " ") for one horizontal line — see Example 3.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Change rows

  • Try $rows = 3, 6, or 8
  • Watch when int overflows

2. BCMath stretch

  • Print 15+ rows with Example 2
  • Compare digit lengths row by row

3. Different multiplier

  • Replace 11 with 12 or 10
  • Observe the new sequence

4. Next in series

  • Continue with Program 49 multiplication triangle
  • Connect to nested-loop patterns

Notes

  • Line count. Total lines printed = $rows (e.g. 5 lines for rows=5).
  • print stays on the line; PHP_EOL advances — mix them carefully.
  • Validate $rows > 0 for interactive programs; $rows = 1 prints one value.
  • Print before multiply — otherwise the first line is not 1.

Quick Takeaway: set $res=1, loop $rows times, echo $res . PHP_EOL, then $res *= 11.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed rows (Example 1)O(n)O(1)
BCMath + fgets(STDIN) (Example 2)O(n) loop; multiply cost grows with digitsO(1)
Single-line output (Example 3)O(n)O(1)
Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Explain $res=1, print, and $res*=11 before coding
  • Use echo $res . PHP_EOL then $res *= 11 each iteration
  • Validate rows ≥ 1 for interactive programs
  • Check fgets(STDIN) return value before using $rows
  • State O(n) time when asked about complexity

❌ Don’t

  • Multiply before printing (skips the first line 1)
  • Use int for many rows (overflows quickly)
  • Hard-code 5 instead of variable $rows
  • Ignore bad console input in user-facing demos
  • Skip the $rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this powers of 11 number pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Initialize

res = 1

Code
03

Multiply ×11

$res *= 11

Logic
n 04

One line/row

n iterations

I/O
O 05

Complexity

O(n)

Analysis

❓ Frequently Asked Questions

Starting from 1, each step multiplies the previous value by 11: 1×11=11, 11×11=121, 121×11=1331, 1331×11=14641.
Yes for larger rows if you use int. Use bcmul() in Example 2 to print more lines safely.
Early results resemble Pascal rows written without spaces. For larger rows, digit carrying appears, so the trick no longer matches simple concatenation.
PHP int overflows after several multiplications by 11 on 32-bit systems. BCMath handles any practical row count as strings.
Yes. Use echo with a trailing space instead of PHP_EOL — see Example 3.
O($n) loop iterations for n rows. BCMath multiplication cost grows with digit length but the loop count stays linear.
Yes. Replace 11 with another integer to explore a different sequence — the loop structure stays the same.
Use trim(fgets(STDIN)) and is_numeric($input) before casting to int.

Did you Know? 🔊

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

Continue to Program 49

Move on to the multiplication number triangle pattern in the PHP number-pattern series.

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.

12 people found this page helpful