Php--use for loop to print half pyramids, pyramids, squares, inverted pyramids, diamonds, Hollow shapes, etc.

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 "*&nbsp";</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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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 "&nbsp;";<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.