A mixed number triangle builds each row from two halves: descending $i..2, then ascending 1..($rows - $i + 1). Every row has exactly $rows digits — with $rows = 5 you get 12345, 21234, 32123, 43212, 54321.
Unlike Program 49 (products $i*$j), this shape concatenates digit sequences — no multiplication.
Approach
How to Solve It
Outer loop picks row $i. Two inner loops fill the descending half, then the ascending half.
Method
Idea
Best for
Two inner loops
Descending $j = $i..2, then ascending $k = 1..($rows-$i+1)
Learning, interviews, demos
Compact trace
Same logic with $rows = 3
Quick dry-runs on paper
Pseudocode
Pseudocode
for i from 1 to rows:
for j from i down to 2:
print j (no newline)
for k from 1 to (rows - i + 1):
print k (no newline)
print newline
Cheat sheet
Goal
Pattern
Outer rows
for ($i = 1; $i <= $rows; $i++)
Descending half
for ($j = $i; $j > 1; $j--) echo $j;
Ascending half
for ($k = 1; $k <= $rows + 1 - $i; $k++) echo $k;
End the row
echo PHP_EOL;
Digits per row
Always $rows
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo $j / echo $k
Stays on the same line
Each digit
echo PHP_EOL
Ends the current line
After both inner loops
Print digits without a newline, then end the row once. Putting echo PHP_EOL inside either inner loop turns the pattern into a vertical list.
Try it
Live Preview
Change the row count and the mixed triangle updates instantly.
Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 25 digits
12345
21234
32123
43212
54321
Trace
Worked Walkthrough
Trace row $i = 3 with $rows = 5 — descending then ascending.
Half
Loop
Prints
Line so far
Descending
$j = 3, 2
3, 2
32
Ascending
$k = 1..(5-3+1) = 1..3
1, 2, 3
32123
When $i = 1, the descending loop never runs — the whole row is just 1..$rows. When $i = $rows, the ascending loop prints only 1, so the row is $rows..(2)1.
Code
PHP Programs
Three complete programs: fixed $rows = 5, CLI input, and a compact $rows = 3 demo. Use View Output to reveal sample results.
Example 1 — Fixed $rows = 5
Hard-coded height — descending half, then ascending half, on every row.
1. Only three rows. Easy to dry-run every descending and ascending step by hand.
2. Same formula. Nothing changes except $rows — proving the pattern scales.
3. Last row is fully descending. Row 3 prints 321 — 3 2 then 1.
Edge Cases & Pitfalls
Check these before calling the solution done.
Descending bound
Stop at 2, not at 1
Use $j > 1 (or $j >= 2). Going down to 1 duplicates the leading 1 from the ascending half.
Ascending length
Use $rows + 1 - $i
Wrong bounds break the fixed width of $rows digits per line.
Newline
Do not put PHP_EOL inside either inner loop
Call echo PHP_EOL only after both halves finish.
Between loops
No break between halves
Run both inner loops back-to-back. A newline between them splits each row in half.
rows = 1
Single value
Output is just 1. A good sanity check for input validation.
fgets
Check is_numeric
Bare (int) casts turn non-numeric strings into 0 — prefer is_numeric before casting.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–3)
O(n²)
O(1)
Each of n rows prints exactly n digits, so total work is n². Only a few integers of extra memory.
Remember
Key Takeaways
Rule: print $i..2, then 1..($rows - $i + 1).
Fixed width: every row has exactly $rows digits.
Break the row: call echo PHP_EOL only after both inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each row $i, print descending $i..2 then ascending 1..($rows-$i+1), then end the line.
Frequently Asked Questions
Each row prints two parts: first a descending sequence from $i down to 2, then an ascending sequence from 1 up to ($rows - $i + 1). Row 2 becomes 2 + 1234 = 21234.
One inner loop prints the descending part ($j = $i down to > 1). The other prints the ascending part ($k = 1..($rows-$i+1)). Splitting them keeps the two halves clear.
When $i = 1, the descending loop $j = $i; $j > 1 never runs. Only the ascending loop prints 1..$rows.
Program 49 prints $i*$j products on each row. Program 50 concatenates digit sequences — descending then ascending — with no multiplication.
Printing a digit stays on the same line. Printing PHP_EOL ends the current row. Digits use echo without a newline; the row break uses echo PHP_EOL after both inner loops.
O(n²) for n rows because each row prints n digits and there are n rows.
Both work for the descending loop — they print the same $i..2 sequence.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int — see Example 2.
🤔
Did you know?
Each row combines two sequences: descending $i..2, then ascending 1..($rows-$i+1). Every row has exactly $rows digits — total prints = n², time O(n²).