A diagonal asterisk pattern prints descending digits n..1 on every row — but replaces the cell where $i == $j with *, so the star marches left each line.
Remember
Rule: for i = 1..n
for j = n..1:
print * if i == j, else print j
5432*
543*1
54*21
5*321
*4321 ← n = 5
After Program 25’s bidirectional triangle, this one keeps a fixed width and swaps one digit for a star per row.
Approach
How to Solve It
Walk $i from 1 to $n; on each row scan $j from $n down to 1 and swap when indices match.
Method
Idea
Best for
Match indices
Inner $j = $n..1; print * when $i == $j
Learning, interviews
Custom symbol
Same loops; swap * for # (or any char)
Quick variants
CLI input
Same loops; read $n with fgets(STDIN)
Interactive practice
Pseudocode
Pseudocode
for i from 1 to n:
for j from n down to 1:
if i == j:
print "*"
else:
print j
print newline
Cheat sheet
Goal
Pattern
Set size
$n = 5;
Outer loop
for ($i = 1; $i <= $n; $i++)
Inner loop
for ($j = $n; $j >= 1; $j--)
Diagonal cell
if ($i == $j) echo "*";
Other cells
else echo $j;
End row
echo PHP_EOL;
Chars per row
Always $n
Printing Numbers vs Starting a New Line
API
Effect
Use for
echo "*" / echo $j
Stays on the same line
Each cell
echo PHP_EOL
Ends the current line
After the inner loop
Print all cells without a newline, then end the row once with echo PHP_EOL. Putting PHP_EOL inside the inner loop prints one character per line.
Try it
Live Preview
Change n and the diagonal asterisk pattern updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · chars = 25
5432*
543*1
54*21
5*321
*4321
Trace
Worked Walkthrough — $n = 3
Trace each outer $i, where $i == $j hits, and the printed row.
$i
Star when $j =
Printed row
1
1 (rightmost)
32*
2
2 (middle)
3*1
3
3 (leftmost)
*21
Inner loop always prints n cells. The star slides left as $i grows. Total chars = n².
Code
PHP Programs
Three complete programs: fixed $n = 5, custom symbol #, and CLI input. Use View Output for sample results.
Example 1 — Fixed $n = 5
Hard-coded size — descending inner loop with * when $i == $j.
Using $j = 1..$n flips the digit order and moves the diagonal to the opposite side.
echo $i
Wrong fill
Printing $i in the else branch repeats the row number. Use echo $j for column digits.
PHP_EOL inside
Vertical cells
If echo PHP_EOL is inside the inner loop, each character lands on its own line.
anti-diagonal
Different condition
$i + $j == $n + 1 draws the other diagonal — a related but different pattern.
n = 1
Single star
Output is just * — a good sanity check.
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
Examples 1–3
O(n²)
O(1)
Each of n rows prints n characters → total n² → O(n²).
Remember
Key Takeaways
Rule: print n..1; swap to * when $i == $j.
Fixed width: every row has exactly $n characters.
echo vs PHP_EOL: cells stay on the line; echo PHP_EOL advances after each row.
Complexity: total chars = n² → O(n²).
One line: descend n..1 each row and swap the cell where row equals column for *.
Frequently Asked Questions
Because $i increases from 1 to $n while $j decreases from $n to 1. The condition $i == $j becomes true at a different position each row.
$j holds the descending column digit (5, 4, 3, 2, 1). When $i != $j, print that digit to fill the row.
Yes. Replace echo "*" with any character or string — see Example 2 with #.
echo $j prints each digit or star on the same line. echo PHP_EOL ends the row after the inner loop finishes.
$j runs from $n down to 1 so each row prints digits in descending order with the star at position $i.
Replace 5 with $n in both loops — see Example 3 and the live preview.
O(n²) for n rows because each row prints n characters using a nested loop.
Use trim(fgets(STDIN)) and check is_numeric($input) before casting to int.
Only one row prints — a single *.
🤔
Did you know?
This pattern prints descending numbers from $n to 1 on each row. When the row index equals the current column value ($i == $j), it prints * instead of the number, creating a diagonal asterisk that moves left each row.