• support@answerspoint.com

How to create Pyramid of asterisks in php

2553

I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. I am facing a problem. Please see my code.

<?php
    $n = $i = 5;
    while ($i--)
    echo str_repeat(' ', $i).str_repeat('*', $n - $i)."\n";
?>

 

  • PHP

  • asked 7 years ago
  • Priyanka Roy

2Answer


0
<html>
    <head>
    <title>create Pyramid of asterisks in php</title>
    </head>
<body>
<pre>
<?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?>
</pre>
</body>
</html>

 

Text in a <pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

  • answered 7 years ago
  • Community  wiki

0

use This program within <center> </center> tag

<center>
<?php

    for($i=1;$i<=5;$i++){
        for($j=1;$j<=$i;$j++){
                    echo "*";
        }
        echo "<br />";
    }

?>
</center>

 

  • answered 7 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 7 years ago
  • viewed 2553 times
  • active 7 years ago

Best Rated Questions