Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

STAR Pattern Printing using PHP for loop


Pattern:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *

Code:
for($i=0; $i<5; $i++)
{
for($j=0; $j<5; $j++)
{
print_r(" * ");
}
print_r("<br/>");
}



Pattern:
*
* *
* * *
* * * *
* * * * *

Code:
for($i=0; $i<5; $i++)
{
for($j=0; $j<=$i; $j++)
{
print_r(" * ");
}
print_r("<br/>");
}

Pattern:
             *
         * *
       * * *
    * * * *
 * * * * *

Code:
for($i=0; $i<5; $i++)
{
for($j=5; $j>=$i; $j--)
{
print_r("&nbsp; "); 
}
for($k=0; $k<=$i; $k++)
{
print_r("*");
}
print_r("<br/>");
}

Pattern:
* * * * *
   * * * *
      * * *
         * *
            *

Code:
$temp = 1;
for($i=5; $i>=1; $i--)
{
for($k=$temp; $k>=0; $k--)
{
print_r(" &nbsp;");
}
for($j=$i; $j>=1; $j--)
{
print_r("*");
}
$temp = $temp + 1;
print_r("<br/>");
}



Pattern:
* * * * *
* * * *  
* * *    
* *      
*

Code:
for($i=1; $i<=5; $i++)
{
for($j=5; $j>=$i; $j--)
{
print_r("*");
}
for($k=1; $k<=$i; $k++)
{
print_r("&nbsp; ");
}
print_r("<br/>");
}

Pattern:
AAA AAB AAC ABA ABB ABC ACA ACB ACC BAA BAB BAC BBA BBB
BBC BCA BCB BCC CAA CAB CAC CBA CBB CBC CCA CCB CCC 

Code:
for($ch1='A' ; $ch1<='C' ; ++$ch1)
{
for($ch2='A' ; $ch2<='C' ; ++$ch2)
{
for($ch3='A' ; $ch3<='C' ; ++$ch3)
{
echo sprintf(" %s%s%s", $ch1, $ch2, $ch3);
}

}
}

Pattern:
A B C D E F G G F E D C B A
    A B C D E F F E D C B A
      A B C D E E D C B A
          A B C D D C B A
              A B C C B A
                 A B B A
                     A A 

Code:
for($i=7; $i>=1; $i--)
{
for($spc=6; $spc>=$i; $spc--)
{
print_r("&nbsp; ");
}

$asci=65;
for($j=1; $j<=$i; $j++)
{
echo sprintf("%c",$asci++);
}
for($j=$i-1; $j>=0; $j--)
{
echo sprintf("%c",--$asci);
}
print_r("<br/>");
}

No comments:

Post a Comment

Bottom Ad [Post Page]