How to print even and odd numbers in php

To print even and odd numbers 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.", ";
    }
?>
How-to-print-even-numbers

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.", ";
    }
?>
How-to-print-odd-numbers
Method:2
Even numbers between 1 to 100
In second method we use ‘for’ loops and ‘if’ condition to print even numbers between 1 to 100. Here loop starts from number 1 and after every count its value increment by 1, when ‘if’ condition will be true, it print even numbers.

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

Odd numbers between 1 to 100
In second method we use ‘for’ loops and ‘if’ condition to print odd numbers between 1 to 100. Here loop starts from number 1 and after every count its value increment by 1, when ‘if’ condition will be true, it print odd numbers.

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

Leave a Reply