w3resource
php Programming Exercies

PHP for loop Exercises: Construct a specific pattern, using nested for loop

PHP for loop: Exercise-3 with Solution

Create a script to construct the following pattern, using nested for loop.

*  
* *  
* * *  
* * * *  
* * * * *

Pictorial Presentation:

PHP for loop Exercises: Construct a specific pattern, using nested for loop

Sample Solution:

PHP Code:

<?php
for($x=1;$x<=5;$x++)
{
   for ($y=1;$y<=$x;$y++)
    {
	 echo "*";
	    if($y< $x)
		 {
		   echo " ";
		 }
     }
 echo "\n";
}
?>

Sample Output:

*                                                           
* *                                                         
* * *                                                       
* * * *                                                     
* * * * *

Flowchart :

Flowchart: Construct a specific pattern, using nested for loop

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Create a script using a for loop to add all the integers between 0 and 30 and display the sum.
Next: Create a script to construct the specific pattern, using a nested for loop.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming