programming-examples/php/Basics/Pattern Problems in PHP.php
2019-11-18 14:44:36 +01:00

407 lines
4.9 KiB
PHP

/* Pattern problems in PHP */
/* 1 */
<?php
for ($row = 1; $row <= 5; $row++)
{
echo "* </br>";
}
?>
/*
Output
*
*
*
*
*
*/
/* Print Triangle of Stars in PHP */
<?php
for ($row = 1; $row <= 5; $row++)
{
for ($col = 1; $col <= 5; $col++)
{
echo '* ';
}
echo "</br>";
}
?>
/*
Output
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
*/
/*Print Star Pattern in PHP */
<?php
for($i=0;$i<=5;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "* ";
}
echo "<br>";
}
?>
/*
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
*/
/* Print Star Pattern in PHP */
<?php
for($i=0;$i<=5;$i++)
{
for($j=5-$i;$j>=1;$j--)
{
echo "* ";
}
echo "<br>";
}
?>
/*
Output
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*/
/* Print Triangle of Stars in PHP */
<?php
for($a=5; $a>=1; $a--)
{
if($a%2 != 0)
{
for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}
for($a=2; $a<=5; $a++)
{
if($a%2 != 0)
{
for($b=5; $b>=$a; $b--)
{
echo "* ";
}
echo "<br>";
}
}
?>
/*
Output
*
* * *
* * * * *
* * *
*
*/
/*
Print Star Pattern in PHP*/
<?php
for ($row = 1; $row <= 5; $row++)
{
for ($col = 1; $col <= ($row > 3 ? 6 - $row : $row); $col++)
{
echo '* ';
}
echo "</br>";
}
?>
/*
Output
*
* *
* * *
* *
*
*/
/* Print Triangle of Stars in PHP*/
<?php
for($i=0;$i<=6;$i++)
{
for($k=6;$k>=$i;$k--)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--)
{
for($k=6;$k>=$i;$k--)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
echo "* ";
}
echo "<br>";
}
?>
/*
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*/
* * * * * * * * * * * * * * *
* * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
for($i=$rows;$i>=1;--$i)
{
for($space=0;$space<$rows-$i;++$space)
echo " ";
for($j=$i;$j<=2*$i-1;++$j)
echo "* ";
for($j=0;$j<$i-1;++$j)
echo "* ";
echo "<br />";
}
}
?>
Program to print below format.
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Hide Answer
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
for($i=$rows;$i>=1;--$i)
{
for($j=1;$j<=$i;++$j)
{
echo $j;
}
echo "<br />";
}
}
?>
Program to print below format
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Hide Answer
<?php
if(isset($_POST['sub']))
{
$rows=$_POST['row'];
for($i=1;$i<=$rows;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo "*";
}
echo "<br />";
}
}
?>
/* Write a program to print below format
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
*/
<?php
if(isset($_POST['sub']))
{
$row=$_POST['row'];
for($i=1;$i<=$row;$i++)
{
for($j=1;$j<=$i;$j++)
{
echo $j;
}
echo "<br />";
}
}
/*
Write a program to print the below format :
1 5 9
2 6 10
3 7 11
4 8 12
*/
<?php
for($i=1;$i<=4;$i++)
{
$i1=$i+4;
$i2=$i+8;
echo $i." ".$i1." ".$i2;
echo "<br />";
}
?>
Write a program for this Pattern:
*****
* *
* *
* *
*****
Hide Answer
<?php
for($i = 1; $i<=5; $i++){
for($j = 1; $j<=5; $j++){
if($i == 1 || $i == 5){
echo "*";
}
else if($j == 1 || $j == 5){
echo "*";
}
else {
echo "&nbsp;&nbsp;";
}
}
echo "<br/>";
}
?>
How to write a Floyd's Triangle?
1
23
456
78910
1112131415
Hide Answer
<?php
$a = 1;
for($i = 1; $i<=5; $i++)
{
for($j = 1; $j<=$i; $j++)
{
echo $a;
$a++;
}
echo '<br/>';
}
?>
Write a program to make a chess:
Hide Answer
<table width="270px" cellspacing="0px" cellpadding="0px" border="1px">
<!-- cell 270px wide (8 columns x 60px) -->
<?php
for($row=1;$row<=8;$row++)
{
echo "<tr>";
for($col=1;$col<=8;$col++)
{
$total=$row+$col;
if($total%2==0)
{
echo "<td height=30px width=30px bgcolor=#FFFFFF></td>";
}
else
{
echo "<td height=30px width=30px bgcolor=#000000></td>";
}
}
echo "</tr>";
}
?>
</table>