• support@answerspoint.com

How to create Pyramid of asterisks in php

2447

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 6 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 6 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 6 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 6 years ago
  • viewed 2447 times
  • active 6 years ago

Best Rated Questions