Powers of 11 Number Pattern in PHP

What You’ll Learn
How to print a powers-of-11 number pattern in PHP using a simple for loop and repeated multiplication.
This is a compact loop exercise that demonstrates state updates across iterations.
⭐ Pattern Output
For 5 rows, the pattern looks like this:
1
11
121
1331
14641Complete PHP Program
Start with $k = 1 and multiply by 11 on each row after the first.
<?php
$k = 1;
for ($i = 1; $i <= 5; $i++) {
if ($i == 1) {
$k = $i;
} else {
$k = $k * 11;
}
echo $k . PHP_EOL;
}🧠 How It Works
Initialize value
$k = 1; stores the current term.
Loop through rows
for ($i = 1; $i <= 5; $i++) controls how many terms to print.
Update term
First row keeps 1. After that, $k = $k * 11; generates the next value.
Print output
echo $k . PHP_EOL; prints one term per line.
Powers of 11 sequence
The loop does n iterations, so runtime is O(n) for n rows.
Variation — User Input (CLI) Version
This version takes number of rows from user input (run from terminal with php):
<?php
echo "Enter number of rows: ";
$rows = (int) trim(fgets(STDIN));
if ($rows < 1) {
echo "Rows must be at least 1" . PHP_EOL;
exit;
}
$k = 1;
for ($i = 1; $i <= $rows; $i++) {
if ($i == 1) {
$k = 1;
} else {
$k *= 11;
}
echo $k . PHP_EOL;
}💡 Tips for Enhancement
Try These
- Use big integer libraries for many rows (avoid overflow)
- Compare output with direct power calculation
pow(11, n) - Format output in columns for readability
- Generate related sequences (powers of 2, powers of 3, etc.)
- Store terms in an array and reuse in UI/table views
Avoid
- Assuming no overflow for very large row counts
- Skipping input validation for negative/zero rows
- Confusing powers-of-11 with exact Pascal rows for large n
- Using browser-only line breaks in CLI-focused examples
Key Takeaways
Each new row multiplies the previous term by 11.
The first five terms are 1, 11, 121, 1331, 14641.
Runtime is linear in row count (O(n)).
The same loop pattern works for other geometric progressions.
❓ Frequently Asked Questions
11^0 = 1, which is the first term of the sequence.Explore More PHP Number Patterns!
Keep practicing with sequence-based and loop-based number problems.
The first few powers of 11 visually resemble Pascal triangle rows, which is why this quick pattern is often used in math-programming demos.
12 people found this page helpful
