Wednesday, December 12, 2012



I got bored so i did a simple php program that generates a centered triangle with table for every spaces and stars. I used a 4 for loops.

First for loops is for the size of the triangle
Second for loops is for the spaces at the left side.
Third for loop is for the stars.
Fourth for loop is for the spaces at the right side.


if you want to change the size just change the value of variable $size


<?php
$size = 5;
echo "<table border = '1'> <tr>";
for ($i = 0; $i < $size; $i++)
        {
            $stars = 1 + 2 * ($i);
            $space = $size - $i;
            for ($j = 0; $j < $space; $j++)
            {
                echo("<td>&nbsp;&nbsp;</td>");
            }
           
            for ($k = 0; $k < $stars; $k++)
            {
                echo "<td>*</td>";
            }
$stars = 1 + 2 * ($i);
            $space = $size - $i;
            for ($j = 0; $j < $space; $j++)
            {
                echo("<td>&nbsp;&nbsp;</td>");
            }

            echo("</tr>");
        }
echo("</table>")
?>
Categories:

0 comments:

Post a Comment

Subscribe to RSS Feed Follow me on Twitter!