Logic To create “Diamond Shape”

I am learning to program and  i was struggling with “logic to create Diamond shape in PHP“. I searched but could not find it, so once i figured it out, i thought i will just write it here.

Diamoand

 
for($i = 0; $i <= 14; $i++ )
{
	 for($j = 1; $j<=1+$i ; $j++ )
	 {
		echo  "*" ;
	 }
	echo "";
}
for($y = 1; $y <= 15; $y++ )
{
	 for($x = 1; $x<=15-$y; $x++ )
	 {
		echo  "*" ;
	 }
	echo "";
}
Result is ....
Blank Diamond
for($i = 0; $i <= 14; $i++ )
{
	 for($j=1; $j<=1+$i;$j++)
	 {
		 if ($j==1)
		 {
			echo"*";
		 }
		 else
		 {
			echo "-";
		 }
		 if ($j==$i+1)
		 {
			echo"*";
		 }
		 else
		 {
			echo"  ";
		 }
	 }
	 echo "	";
}
for($i = 0; $i <= 14; $i++ )
{
	 for($j=1; $j<=14-$i;$j++)
	 {
		 if ($j==1)
		 {
			echo"*";
		 }
		 else
		 {
			echo "-";
		 }
		 if ($j==14-$i)
		 {
			echo"*";
		 }
		 else
		 {
			echo"  ";
		 }
	 }
	 echo "	";
}
Resule as...
Midline in Diamond shape
$n=14;
 for($i = 0; $i <= 13; $i++ )
 {
	 for($j=1; $j<=1+$i;$j++)
	 {
		 if ($j==1)
		 {
			echo"*";
		 }
		 else
		 {
			echo "  ";
		 }
		 if ($j==$i+1)
		 {
			echo"*";
		 }
		 else
		 {
			echo"  ";
		 }
	 }
	 echo "	";
	 if ($i+1==$n)
	 {
		 for($k = 0; $k <= $n*2; $k++ )
		 {
			echo"*";
		 }
	 }
 }
 echo "";
 for($i = 0; $i <= $n; $i++ )
 {
	 for($j=1; $j<=$n-$i;$j++)
	 {
		 if ($j==1)
		 {
			echo"*";
		 }
		 else
		 {
			echo "  ";
		 }
		 if ($j==$n-$i)
		 {
			echo"*";
		 }
		 else
		 {
			echo "  ";
		 }
	 }
	 echo "	";
 }
result as
Plus in Diamond
$n=14;$m=7;$l=2;
for($i = 0; $i <= 13; $i++ )
{
	 for($j=1; $j<=1+$i;$j++)
	 {
		 if ($j==1)
			echo"*";
		 else
			echo"&nbsp;&nbsp;";
		 if ( $i % 2==$j)
		 {
			 for ($j=5;$j<=$i+1;$j++)
			 {
				 if ( $i % 2==$j)
					echo "*";
			 }
		 }
		 if ($j==$i+1)
			echo"*";
		 else
			echo"&nbsp;&nbsp;";
	 }
	 echo "<br >";
	 if ($i+1==$n)
	 {
		 for($k = 0; $k <= $n*2; $k++ )
		 {
			echo"*";
		 }
	 }
}
echo "<br >";
for($i = 0; $i <= $n; $i++ )
{
	for($j=1; $j<=$n-$i;$j++)
	{
		if ($j==1)
			echo"*";
		else
			echo"&nbsp;&nbsp;";
		if ( $i % 2==$j)
		{
			for ($j=14;$j<=$i+1;$j++)
			{
				if ( $i % 2==$j)
					echo "*";
			}
		}
		if ($j==$n-$i)
			echo"*";
		else
			echo"&nbsp;&nbsp;";
	}
	echo "<br >";
}

Result as

i am still learning if their is any mistake do let me know.

One reply on “Logic To create “Diamond Shape””

Comments are closed.