PHP code numeric polygon with odd numbers

We get the output of a numeric polygon using php ‘Loops’. We apply the ‘Loops’ with logic in php coding to get the output of a polygon that shoes only ‘odd’ numbers.
Here we use eight ‘Loops’.

  1. First and fifth are main and other are nested in these.
  2. First ‘Loop’ is used for number of lines in upper triangle and fifth loop is used for number of lines in lower triangle.
  3. Second ‘Loop’ is used to print spaces.
  4. Third ‘Loop’ is used for left side right angle triangle that will show counting according to line number and fourth for loop is used for right side right angle triangle that will show reverse counting according to last line number.
  5. Sixth for ‘Loop’ is used to print spaces and seventh is used to show reverse counting in decreasing order in lower left triangle(right angle triangle).
  6. Eighth is used to show counting in reverse order in right side triangle(right angle triangle).

After get a polygon we apply if-else in loop conditions and this will show only ‘odd’ numbers among all the polygon numbers. Here $n is used for number of lines in first loop and number of spaces in second loop.

<?php
$m=1; $n=9; $z=9;
for($i=1; $i<=$n; $i++){
  for($j=$i; $j<=$n-1; $j++){
    echo "&nbsp;&nbsp;&nbsp;";
    }
  for($k=1; $k<=$m; $k++){
    if($k%2==0){
      echo "&nbsp;&nbsp;&nbsp;";
      }
    else{
      echo $k ."&nbsp";
      }
    }
  for($c=$m; $c>1; $c--){
    if(($c-1)%2==0){
      echo "&nbsp;&nbsp;&nbsp;";
      }
    else{
      echo $c-1 ."&nbsp";
      }
    }
    echo "<br>";
    $m++;
}
for($i=1; $i<=$n; $i++){
  for($j=1; $j<=$i; $j++){
    echo "&nbsp;&nbsp;&nbsp;";
    }
  for($k=1; $k<$z; $k++){
    if($k%2==0){
      echo "&nbsp;&nbsp;&nbsp;";
      }
    else{
      echo $k ."&nbsp";
      }
    }
  for($c=$z-2; $c>=1; $c--){
    if($c%2==0){
      echo "&nbsp;&nbsp;&nbsp;";
      }
    else{
      echo $c ."&nbsp";
     }
  }
  echo "<br>";
  $z--;
}
?>
how-to-make-a-numeric-polygon-shows-only-odd-numbers

Leave a Reply