How to print stars along with there equal number

We can print a star pyramid pattern in php along with there row number using ‘for’ loops. To print this pattren we will use a ‘for’ loop to print rows. The first ‘for’ loop will indicate the number of rows. Then we use ‘if’ condition to check the even & odd values. We will print number value first in odd number and then stars and vise versa we will print star first for even number values. We will print the stars using nested ‘for’ loops in main ‘for’ loop.

<?php
for($a=1; $a<=10; $a++){
  if($a%2 != 0){
    echo $a;
    for($b=1; $b<=$a; $b++){
	echo '*';
	}
    }
    else{
	for($b=1; $b<=$a; $b++){
		echo '*';
		}
		echo $a;
    }
    echo "
";
}
?>
Stars with numbers

Add a Comment

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