PHP for print Pyramid.

Here's an example of how to print a pyramid in PHP using nested for loops:


<?php


// set the number of rows in the pyramid

$num_rows = 5;


// loop through each row

for ($i = 0; $i < $num_rows; $i++) 

{

// print the spaces before the asterisks

  for ($j = 0; $j < $num_rows - $i - 1; $j++) 

  {

    echo " ";

  }

// print the asterisks for the current row

  for ($k = 0; $k < 2 * $i + 1; $k++) 

  {

    echo "*";

  }


  // move to the next line

  echo "\n";

}


?>


Output: 

    *
   ***
  *****
 *******
*********




Post a Comment

Previous Post Next Post
© 2023 Developed and Design By
NILESH NISHAD