Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display the stars in this way > *
					                       *
					                        *
					                         *
						                      *

But when i tried with the below code i'm getting it as right angle triangle, please can any one help to get the desired answer.


What I have tried:

for ($i=1; $i<=4; $i++)  
            {  
             for ($j=1; $j<=$i; $j++)  
            {  
               echo '* ';  
            }  
               echo "</br>";  
            }
Posted
Updated 3-Apr-18 5:48am
v2

1 solution

You want to have an inner loop for creating the spaces. Also, the innerloop has a different count for the spaces, the first one doesn't need to be indented. Here is a version for a console output:
PHP
for ($i=1; $i<=4; $i++)  
{  
    for ($j=2; $j<=$i; $j++)  
        echo ' ';  
    echo '*';
    echo "\r\n";  
}
if you want to output it in HTML you might want to use this version:
PHP
for ($i=1; $i<=4; $i++)  
{  
    for ($j=2; $j<=$i; $j++)  
        echo '&nbsp;';  
    echo '*';
    echo "</br>";  
}
Because in HTML consecutive spaces are ignored.
 
Share this answer
 
Comments
1234Amit 3-Apr-18 10:29am    
thank you sir, it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900