3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

forループの練習問題(数字でピラミッドを作る)  初級〜中級・解答編

Last updated at Posted at 2017-02-13

#問題
下記のリンクの練習問題の解答(PHPで記述)になります。
http://qiita.com/takuya-kodama/items/32edabf2c27c4b4690a0

解答よりも良いコードがありましたらコメント等で教えていただければと思います!

#解答

example.php
<?php

#問題1
for($x = 2 ; $x >= 0 ; $x -- ){
  for($y = 3 ; $y >= $x+1 ; $y --){
    echo $y-$x;
  }
  echo "<br>";
}

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


#問題3
$z = 1;
for($x = 1 ; $x <= 3 ; $x ++){
  for($y = 1 ; $y <= 3 ; $y ++){
    echo $z;
    $z ++;
  }
  echo "<br />";
}

#問題4
$z = 1;
for($x = 1 ; $x <= 3 ; $x ++){
  for($y = 1 ; $y <= $x ; $y ++){
    echo $z;
    $z ++;
  }
  echo "<br />";
}

#問題5
$z = 9;
for($x = 1 ; $x <= 3 ; $x ++){
  for($y = 1 ; $y <= 3 ; $y ++){
    echo $z;
    $z --;
  }
  echo "<br />";
}

#問題6
$z = 6;
for($x = 1 ; $x <= 3 ; $x ++){
  for($y = 1 ; $y <= $x ; $y ++){
    echo $z;
    $z --;
  }
  echo "<br />";
}

#問題7
for($x = 1 ; $x <= 3 ; $x++){
  for($y = 1 ; $y <= $x ; $y++){
    echo $y;
  }
  echo "<br />";
}


#問題8
for($x = 3 ; $x >= 1 ; $x--){
  for($y = 1 ; $y <= $x ; $y++){
    echo $y;
  }
  echo "<br />";
}

#問題9
for($x = 1 ; $x <= 3 ; $x++){
  for($y = 3 ; $y >= $x ; $y--){
    echo $y;
  }
  echo "<br />";
}

#問題10
for($x = 3 ; $x >= 1 ; $x--){
  for($y = 3 ; $y >= $x ; $y--){
    echo $y;
  }
  echo "<br />";
}

#問題11
for($x = 1 ; $x <= 3 ; $x++){
  echo $x;
  for($y = 1 ; $y <= 2 ; $y ++){
    echo $y;
  }
  echo "<br />";
}

?>

3
2
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?