How to print even and odd numbers in php
July 27, 2016
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.", "; } ?> |
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.", "; } ?> |
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.
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.", "; } } ?> |
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.", "; } } ?> |