Thursday, 5 September 2013

Print Diamond Pattern through php

//[Note: Grow Diamond size by increasing value of Diamond_Size variable.
//for example Diamond_Size=29
//please set Diamond_Size variable to odd numbers only.]

<?php
$Diamond_Size=19;     

if($Diamond_Size%2==1)
{
printUpperTriangle($Diamond_Size);
//echo "inside";
printLowerTriangle($Diamond_Size);
}
else
{
echo "Please set limit to some odd number";
}

    function printUpperTriangle($limit)
    {
        $sp=$limit/2;
        for($i=1,$space=$sp;$i<=$limit;$i+=2,$space--)
        {
            if($space>=0)
            {
            printSpace($space);
                for($j=1;$j<=$i;$j++)
                {
                echo "*";
                }
            }
        echo "<br/>";
        }
    }
    function printLowerTriangle($limit)
    {
        $sp=$limit/2;
        for($i=$limit-2,$space=1;$i>0;$i-=2,$space++)
        {
            if($space<=$sp)
            {
                echo "&nbsp;&nbsp;";
            printSpace($space);
                for($j=1;$j<=$i;$j++)
                {
                echo "*";
                }
            }
        echo "<br/>";
        }
    }
    function printSpace($space)
    {
        for($i=$space;$i>0;$i--)
        {
        echo "&nbsp;&nbsp;";
        }
    }

?>

No comments:

Post a Comment