This post contains a total of 7+ Hand-Picked PHP Odd-Even Program Examples with Source Code. All the Odd-Even programs are made using PHP Programming Language.
You can use the source code of these programs for educational purpose with credits to the original owner.
Related Posts
Click a Code to Copy it.
1. By Swati
Made by Swati. Simple PHP program to check if a number is odd or even. Add your own number in $num. ( Source )
10 Even number
<?php
$num=10;
if($num%2==0)
{
echo "Even number";
}
else
{
echo "Odd number";
}
?>
2. By Sunny
Made by Sunny. Odd Even program using Ternary Operators. Add your number in $check. ( Source )
20 Number is Even
<?php
$check=20;
/*
Here we use ternary operator , Firstly we know about its syntax
condition ? true: false
Here our condition is $check%2==0 , means if we divide $check with 2 then remainder is 0; if its true than we write Number is Even after ? symbol, Else we write Number is Odd after : symbol
*/
echo ($check%2==0 ? 'Number is Even' : 'Number is Odd');
?>
3. By Sunny
Another Odd even Checker by Sunny using Switch Case. ( Source )
11 11 is Odd Number
<?php
$x=11;
/* Here is Our Variable named x, in which we store a value, We provide int value only(without decimal point) when we echo $x%2, then iyt will return a value that is 0 or 1, Depend on what remainder is, when we divide variable value by 2
so we write it in switch, & it will match it with case value.
*/
switch($x%2)
{
case 0:
echo $x ." is Even Number";
break;
case 1:
echo $x ." is Odd Number";
break;
}
?>
4. By Javier Ballesteros
Made by Javier Ballesteros. This program will sort out the odd and even numbers from an 5×5 array. ( Source )
Odd: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, Even: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24,
<?php
// This code was made without using PHP functions.
$arr = [
[0,1,2,3,4],
[5,6,7,8,9],
[10,11,12,13,14],
[15,16,17,18,19],
[20,21,22,23,24]
];
$even = [];
$odd = [];
$e = 0;
$o = 0;
for ($i=0;$i<5;$i++) {
for ($j=0;$j<5;$j++) {
if ($arr[$i][$j]%2 == 0) {
$even[$e++] = $arr[$i][$j];
}
else {
$odd[$o++] = $arr[$i][$j];
}
}
}
echo "Odd: ";
fun($odd, $o);
echo "<br/>Even: ";
fun($even, $e);
function fun ($a, $c) {
for ($i=0;$i<$c;$i++) {
echo $a[$i].", ";
}
}
?>
5. By jamilxt
Made by jamilxt. Prints the odd and the even numbers from range 1 to 20 using For Loop. ( Source )
20 is even. 19 is odd. 18 is even. 17 is odd. 16 is even. 15 is odd. 14 is even. 13 is odd. 12 is even. 11 is odd. 10 is even. 9 is odd. 8 is even. 7 is odd. 6 is even. 5 is odd. 4 is even. 3 is odd. 2 is even. 1 is odd.
<?php
for ($count = 20; $count > 0; $count--) {
if ($count % 2 == 0) {
echo "{$count} is even. <br/>";
} else {
echo "{$count} is odd. <br/>";
}
}
?>
6. By Andre Golban
Made by Andre Golban. The program outputs all the odd and even numbers from 0 to 100. ( Source )
It's even numbers: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 It's odd numbers: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
<?php
$even = [];
$odd = [];
for ($i=1; $i<=100; ++$i){
if($i%2==0){
$even[] = $i;
} else {
$odd[] = $i;
}
}
echo "It's even numbers:"."<pre>";
foreach ($even as $even_n){
echo $even_n." ";
}
echo "<pre>";
echo "It's odd numbers:"."<pre>";
foreach ($odd as $odd_n){
echo $odd_n." ";
}
?>
7. By Ntirpang Louis
Made by Ntirpang Louis. ( Source )
Odd numbers. 1 >>> 3 >>> 5 >>> 7 >>> 9 >>> Start of Even numbers. 0 >>> 2 >>> 4 >>> 6 >>> 8 >>> 10 >>>
<?php
echo "<b>Odd numbers.</b><br>";
for($x=1;$x<=100;$x+=2){
echo "<br>";
echo $x;
echo "<br><br>>>><br>";
}
echo "<br> <br> <br><b> Start of Even numbers.</b> <br>";
for($x=0;$x<=100;$x++){
if($x%2!==0){
continue ;
}
echo "<br>";
echo $x;
echo "<br><br>>>><br>";
}
?>
8. By Praveen Soni
Made by Praveen Soni. ( Source )
37 odd number
<?php
$a=34;
if($a%2==0)
echo "even number";
else
echo " odd number";
?>