I want to make a triangle like this with php , but i try in local still fail how to code like the example above
<?php
$star=10;
for($a=1; $a<=$star; $a++){
for($c=$star; $c>=$a; $c-=1){
echo "*";
}
echo "<br>";
}
?>
I have value 1225441. How to create output like this with looping php?
1000000
200000
20000
5000
400
40
1
3
Answers
You had the right idea with a for loop.
You just need to know how long your input is, and pad the output with the length of your input, gradually getting smaller with each iteration.
This creates the output of:
If it’s hard for you to think about how to do it with PHP, then think first about the general way to do it, I mean the algorithm. You can represent it with a flow diagram. Then, you just need to translate it to PHP code.
And the result it can be something like this:
Yet another solution as a one-liner.
Output