111. PHP Program to Print Number triangle.
How does this program work?
- This program is used to print Number Triangle using PHP.
Here is the code
<html>
<head>
<title>PHP Program To print Triangle Pattern</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num" value="" placeholder="Enter no.of rows"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$rows = $_POST['num'];
$space = 10;
$k = 0;
$count = 0;
$count1 = 0;
for($i=1; $i<=$rows; ++$i)
{
for($space=1; $space <= $rows-$i; ++$space)
//for loop for spaces
{
echo " ";
echo " ";
++$count;
}
while($k != 2*$i-1)
{
if ($count <= $rows-1)
{
echo "\n ".$i+$k;
++$count;
}
else
{
++$count1;
echo " ".$i+$k-2*$count1;
}
++$k;
}
$count1 = $count = $k = 0;
echo "\n"."</br>";
}
return 0;
}
?>
</body>
</html>