How to check if a number is odd or even without modulus operator in PHP

1 Answer

0 votes
function is_even($n) { 
    return ((int)($n / 2) * 2 == $n); 
} 
  
$n = 13; 

$s = (is_even($n)) ? "Even" : "Odd"; 
 
echo $s;
 
   
/*
run:
        
Odd
       
*/

 



answered Mar 28, 2019 by avibootz

Related questions

...