Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@ta-k

forループの練習問題（数字でピラミッドを作る）　 上級・解答編

More than 3 years have passed since last update.

問題

http://qiita.com/takuya-kodama/items/5a1dd8da56e7a87ec58f

解答

```<?php

#問題１
for(\$a = 1 ; \$a <= 3; \$a++){
for(\$b = 4; \$b >= \$a+1 ; \$b--){
echo \$b-\$a;
}
echo "<br />";
}

#問題２
for(\$a = 3 ; \$a >= 1 ; \$a --){
for(\$c = 2 ; \$c >= \$a ; \$c --){
echo "*";
}

for(\$b = 1 ; \$b <= \$a ; \$b ++){
echo \$b;
}
echo "<br />";
}

#問題３
\$x = 6;
for(\$a =1 ; \$a <= 3 ; \$a ++){
for(\$b = 1 ; \$b<= \$a ; \$b++){
echo \$x;
\$x --;
}
echo "<br />";
}

#問題４
for(\$a = 4 ; \$a >= 1 ; \$a--){
for(\$b = 3 ; \$b >= \$a ; \$b--){
echo "*";
}
for(\$c = 1 ; \$c <=\$a ; \$c++){
echo \$c;
}
for(\$d = \$c-2 ; \$d >=1  ; \$d --){
echo \$d;
}
echo "<br />";
}

#問題５
for(\$a = 1 ; \$a <= 9 ; \$a ++){
if(\$a <= 5){
for(\$b = 4 ; \$b >= \$a ; \$b --){
echo "*";
}
for(\$c = 1; \$c <= \$a ; \$c++){
echo \$c;
}
for(\$d =\$c -2 ; \$d >= 1 ; \$d--){
echo \$d;
}
echo "<br />";
}
else if(\$a >=6 ){
for(\$e = 6 ; \$e <= \$a ; \$e++){
echo "*";
}
for(\$f = 1 ; \$f <=10-\$a  ; \$f++){
echo \$f;
}
for(\$g = \$f-2 ; \$g >=1  ; \$g --){
echo \$g;
}
echo "<br />";
}
}

#問題６
for(\$a = 5 ; \$a >= 1 ; \$a --){
for(\$b = 2 ; \$b <= \$a; \$b ++){
echo "*";
}
for(\$c = 5 ; \$c >= \$a ;\$c --){
echo \$c;
}
for(\$d = \$c+2 ; \$d <= 5   ; \$d ++){
echo \$d;
}
echo "<br />";
}

?>

```
3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away