Php--use for loop to print half pyramids, pyramids, squares, inverted pyramids, diamonds, Hollow shapes, etc.
Last Update:2016-07-23
Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Half Pyramid</p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">Number of control layers</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">For ($i =1; $i <= $n; $i + +) {</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">Control the number of "*" per layer</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">For ($j =1; $j <= $i; $j + +) {</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "*";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">}</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "<br/>";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">}</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">2. pyramids</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">For ($i =1; $i <= $n; $i + +) {</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">For ($k =1; $k <= $n-$i; $k + +) {</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "the"; Output spaces</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">}</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">For ($j =1; $j <= ($i-1) *2+1; $j + +) {</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "*";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">}</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "<br/>";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">}</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">3, Square</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">For ($i =1; $i <= $n; $i + +) {</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">For ($k = $n; $k >=0; $k--) {</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "* ";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">}</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><span style="white-space: pre;">Echo "<br/>";</span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">}</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">4/inverted Pyramid</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;<br>For ($i = $n; $i >=1; $i--) {<br><span style="white-space: pre;">For ($k =1; $k <= $n-$i; $k + +) {<br><span style="white-space: pre;">Echo "the";<br><span style="white-space: pre;">}<br><span style="white-space: pre;">For ($j =1; $j <= ($i-1) *2+1; $j + +) {<br><span style="white-space: pre;">Echo "*";<br><span style="white-space: pre;">}<br><span style="white-space: pre;">Echo "<br/>";<br>}<br></span></span></span></span></span></span></span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">5. Diamond</span></span></p></p><p><p> <span style="font-size:14px;"><span style="font-family:arial;"> $n =20; <br> For ($i =1; $i <= $n; $i + +) {<br> <span style="white-space:pre;">for ($k =1; $k <= $n-$i; $k + +) {<br> <span style=" white-space:pre; " ">echo"; <br> <span style="white-space:pre" , ">} <br> <span style=" white-space:pre ">for ($j =1; $j <= ($i-1) *2+1; $j + +) {<br> <span style="white-space:pre;">echo "*"; <br> <span style="white-space:pre" , ">} <br> <span style=" white-space:pre; ">echo" <br/> "; <br>} <br> for ($i = $n; $i <= $n; $i-) {<br> <span style="white-space:pre; >for ($k =1; $k <= $n-$i; $k + +) {<br> <span style=" white-space:pre; ">echo"; <br> <span style="white-space:pre" , ">} <br> <span style=" white-space:pre ">for ($j =1; $j <= ($i-1) *2+1; $j + +) {<br> <span style="white-space:pre;">echo "*"; <br> <span style="white-space:pre;"> <br> <span style="white-space:pre;">echo "<br/>"; <br> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">}</span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">6. Hollow Diamond</span></span></p></p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;<br>For ($i =1; $i <=5; $i + +) {<br>Print space<br>For ($j =1; $j <=5-$i; $j + +) {<br>Echo " ";<br>}<br>Print * Number<br>For ($k =1; $k <=2* $i-1; $k + +) {<br>The last line after the first line is printed * Connection ($i ==1 | | $i ==5)<br>If ($i ==1) {//remove $i==5 and empty the middle<br>Echo "*";<br>}else{<br>How to play the blanks and the * number problem<br>If ($k ==1 | | $k ==2* $I-1) {<br>Echo "*";<br>}else{<br>Echo " ";<br>}<br>}<br>}<br>Echo "<br/>";<br>}<br>Reverse Empty<br>For ($i =5; $i >=0; $i-) {<br>Print space<br>For ($j =0; $j <=5-$i; $j + +) {<br>Echo " ";<br>}<br>Print * Number<br>For ($k =1; $k <=2* $i-3; $k + +) {<br>How to play the blanks and the * number problem<br>If ($k ==2* $i-3 | | $k ==1) {<br>Echo "*";<br>}else{<br>Echo " ";<br>}<br>}<br>Echo "<br/>";<br>}<br></span></span></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;">7. Hollow Pyramid</span></span></p></p><p><span style="font-size: 14px;"><span style="font-family: Arial;">$n = 5;<br>For ($i =1; $i <=5; $i + +) {<br>Print space<br>For ($j =1; $j <=5-$i; $j + +) {<br>Echo " ";<br>}<br>Print * Number<br>For ($k =1; $k <=2* $i-1; $k + +) {<br>After the first line is printed, the last line is called * connected<br>If ($i ==1 | | $i ==5) {<br>Echo "*";<br>}else{<br>How to play the blanks and the * number problem<br>If ($k ==1 | | $k ==2* $I-1) {<br>Echo "*";<br>}else{<br>Echo " ";<br>}<br>}<br>}<br>Echo "<br/>";<br>}<br></span></span></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><p><span style="font-size: 14px;"><span style="font-family: Arial;"><br></span></span></p></p><p><span style="font-size: 14px;"><span style="font-family: Arial;">8. Inverted Hollow Pyramid<br><span style="white-space: pre;">$n = 5;<br>For ($i =1; $i <=5; $i + +) {<br>Print space<br>For ($j =1; $j <=5-$i; $j + +) {<br>Echo " ";<br>}<br>Print * Number<br>For ($k =1; $k <=2* $i-1; $k + +) {<br>After the first line is printed, the last line is called * connected<br>If ($i ==1 | | $i ==5) {<br>Echo "*";<br>}else{<br>How to play the blanks and the * number problem<br>If ($k ==1 | | $k ==2* $I-1) {<br>Echo "*";<br>}else{<br>Echo " ";<br>}<br>}<br>}<br>Echo "<br/>";<br>}<br>Reverse<br>For ($i =5; $i >=0; $i-) {<br>Print space<br>For ($j =0; $j <=5-$i; $j + +) {<br>Echo " ";<br>}<br>Print * Number<br>For ($k =1; $k <=2* $i-3; $k + +) {<br>Echo "*";<br>}<br>Echo "<br/>";<br>}</span></span></span></p><p><p>Php--use for loop to print half pyramids, pyramids, squares, inverted pyramids, diamonds, Hollow shapes, etc.</p></p></span>