Number Triangle
Number Triangle
Number triangle in PHP can be printed using for and foreach loop. There are a lot of patterns in number triangle. Some of them are show here.
Pattern1
<?php
$k=1;
for($i=0;$i<4;$i++){
for($j=0;$j<=$i;$j++){
echo $k." ";
$k++;
}
echo "<br>";
}
?>
<?php
$k=1;
for($i=0;$i<5;$i++){
for($j=0;$j<=$i;$j++){
if($j%2==0)
{
$k=0;
}
else
{
$k=1;
}
echo $k." ";
}
echo "<br>";
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo $j;
}
echo "<br>";
}
?>
<?php
for($i=0;$i>=5;$i++){
for($j=1;$j>=$i;$j++){
echo $i;
}
echo "<br>";
}
?<