• How to print stars along with there equal number?

    Answered

    How to print stars along with there equal number?

    Bronze Member Asked on August 26, 2016 in Php.
    Add Comment
  • 1 Answer(s)
    Best answer

    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 ”
    “;
    }
    ?>

    HOW TO PRINT STARS ALONG WITH THERE EQUAL NUMBER

    Bronze Member Answered on August 26, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.