PHP Programming Code To Create Pyramid and Pattern

PHP Programming Code To Create Pyramid and Pattern

PHP Programming To Create Pyramid and Pattern. Create triangle, Floyd’s Triangle,Pascal’s triangle using for loop. create Triangle using php language.

PHP Program to print half pyramid using *

Example 1: PHP Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *
$rows = 5; // add your number of row
for($index = 1; $index <= $rows; ++$index)
{
for($j=1; $j<=$index; ++$j)
{
echo ("*&nbsp;"); //add space in *
}
echo ("<br>"); // brake line
}

Example 2: PHP Program to print half pyramid a using numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
$rows = 5; // add your number of row
for($index = 1; $index <= $rows; ++$index)
{
for($j=1; $j<=$index; ++$j)
{
echo ($j."&nbsp;"); //add space in numbers
}
echo ("<br>"); // brake line
}
Example 3: PHP Program to print half pyramid using alphabets

A
B B
C C C
D D D D
E E E E E
$alphabet = 'A';
$last_alphabet = 'E'; // add your alphabet as last alphabet
$key = (ord($last_alphabet) - ord($alphabet)) + 1;
for($index=1; $index <= $key; ++$index)
{
for($j=1;$j<=$index;++$j)
{
echo ($alphabet."&nbsp;"); //add space in alphabet
}
++$alphabet;
echo ("<br>"); // brake line
}

PHP Programs to print inverted half pyramid using numbers and *

Example 4: Inverted half pyramid using *

* * * * *
* * * *
* * * 
* *
*
$rows = 5; // add your number of row
for($index=$rows; $index>=1; --$index)
{
for($j=1; $j<=$index; ++$j)
{
echo ("*&nbsp;"); //add space in *
}
echo ("<br>"); // brake line
}

Example 5: PHP Programs to Inverted half pyramid using numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
$rows = 5; // add your number of row
for($index = $rows; $index>=1; --$index)
{
for($j = 1; $j<=$index ; ++$j)
{
echo ($j);
}
echo ("<br>"); // brake line
}

PHP Programs to display pyramid and inverted pyramid using numbers and *

Example 6: PHP Program to print full pyramid using *

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
$keys = 0;
$rows = 5; // add your number of row
for($index = 1; $index  <= $rows; ++$index, $keys=0)
{
for($space = 1; $space <= $rows-$index ; ++$space)
{
echo ("&nbsp;&nbsp;&nbsp;"); //add space
}
while($keys != 2*$index -1)
{
echo ("*&nbsp;"); //add space in *
++$keys;
}
echo ("<br>"); // brake line
}

Example 7: PHP Program to print pyramid using numbers

        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
$key = $count = $count_temp = 0;
$rows = 5; // add your number of row

for($index = 1; $index <= $rows; ++$index)
{
for($space=1; $space <= $rows - $index; ++$space)
{
echo ("&nbsp;&nbsp;&nbsp;"); //add space
++$count;
}

while($key != 2 * $index-1)
{
if ($count <= $rows-1)
{
echo ($index + $key."&nbsp;"); //add space in numbers
++$count;
}
else
{
++$count_temp;
echo ($index + $key-2 * $count_temp."&nbsp;"); //add space in numbers
}
++$key;
}
$count_temp = $count = $key = 0;
echo ("<br>"); // brake line
}

Example 8: PHP Program to print Inverted full pyramid using *

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
$rows = 5; // add your number of row
for($index = $rows; $index  >= 1; --$index )
{
for($space = 0; $space < $rows - $index ; ++$space)
echo ("&nbsp;&nbsp;"); //add space

for($j=i; $j <= 2* $index - 1; ++$j)
echo ("*&nbsp;");  //add space in *

for($j=0; $j < -1; ++$j) //add space in *
echo ("*&nbsp;");

echo ("<br>"); // brake line
}

Example 9: PHP Program to Print Pascal’s triangle

           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1 
$coef = 1;
$rows = 6; // add your number of row

for($index = 0; $index < $rows; $index++)
{
for($space = 1; $space <= $rows - $index; $space++)
echo ("&nbsp;&nbsp;"); //add space

for($j=0; $j <= $index; $j++)
{
if ($j == 0 || $index == 0)
$coef = 1;
else
$coef = $coef*($index - $j+1)/$j;

echo ($coef."&nbsp;"); //add space in number
}
echo ("<br>"); // brake line
}

Example 10: PHP Program to Print Floyd’s Triangle.

1
2 3
4 5 6
7 8 9 10
$number= 1;
$rows = 5; // add your number of row

for($index = 1; $index <= $rows; $index++)
{
for($j = 1; $j <= $index; ++$j)
{
echo ($number."&nbsp;");  //add space in number
++$number;
}

echo ("<br>"); // brake line
}

Leave a Comment

Your email address will not be published. Required fields are marked *

70  +    =  77

We accept paid advance and paid guest Posting on our Site : Contact US