How to check if a number is cyclops (number with odd number of digits and zero in the center) in PHP

1 Answer

0 votes
function isCyclopsNumber($n) {
    if ($n == 0) {
        return true;
    }
    
    $m = $n % 10;
    $count = 0;
    
    while ($m != 0) {
        $count++;
        $n = (int)($n / 10);
        $m = $n % 10;
    }
    
    $n = (int)($n / 10);
    $m = $n % 10;
    
    while ($m != 0) {
        $count--;
        $n = (int)($n / 10);
        $m = $n % 10;
    }
    
    return $n == 0 && $count == 0;
}
        
echo (isCyclopsNumber(209) ? "yes" : "no"),"\n";
echo (isCyclopsNumber(18037) ? "yes" : "no"),"\n";
echo (isCyclopsNumber(5604) ? "yes" : "no"),"\n";





/*
run:

yes
yes
no

*/

 



answered Apr 10, 2023 by avibootz
...