• HOW TO PRINT EVEN AND ODD NUMBERS IN PHP?

    Answered

    HOW TO PRINT EVEN AND ODD NUMBERS IN PHP?

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

    We can print even and odd numbers in php. To print this we can use different types of method.
    Method:1
    Even numbers between 1 to 100
    In first method we use ‘for’ loops to print even numbers between 1 to 100. Here loop starts from number 2 and after every count its value increment by 2 for print even numbers.

    <?php
    echo “Even numbers between 1 to 100 :- “;
    for ($j=2; $j<=100; $j+=2){
    echo $j.”, “;
    }
    ?>

    <?php
    echo “Even numbers between 1 to 100 :- “;
    for ($j=1; $j<=100; $j++){
    if( $j%2==0){
    echo $j.”, “;
    }
    }
    ?>

     

    Even numbers between 1 to 100

     

    Odd numbers between 1 to 100
    In first method we use ‘for’ loops to print odd numbers between 1 to 100. Here loop starts from number 1 and after every count its value increment by 2 for print odd numbers.
    <?php
    echo “Odd numbers between 1 to 100 :- “;
    for ($j=1; $j<=99; $j+=2){
    echo $j.”, “;
    }
    ?>

    <?php
    echo “Odd numbers between 1 to 100 :- “;
    for ($j=1; $j<=100; $j++){
    if( $j%2!=0){
    echo $j.”, “;
    }
    }
    ?>

    Odd numbers between 1 to 100

     

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

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