Check whether a number has consecutive 0’s in PHP

Share on facebook
Share on twitter
Share on linkedin
Share on twitter
Share on tumblr

In this PHP Program, we will check if the given number has consecutive 0’s .

PHP Program to check whether a number has a consecutive 0’s

<?php
function result($N, $K)
{
    $z = toK($N, $K);
    if (check($z))
        print("Yes");
    else
        print("No");
}
  
function toK($N, $K)
{
    $w = 1;
    $s = 0;
    while ($N != 0)
    {
        $r = $N % $K;
        $N = (int)($N / $K);
        $s = $r * $w + $s;
        $w *= 10;
    }
    return $s;
}
  
function check($N)
{
    $fl = false;
    while ($N != 0)
    {
        $r = $N % 10;
        $N = (int)($N / 10);
  
        // If there are two consecutive 
        // zero then returning false
        if ($fl == true and $r == 0)
            return false;
        if ($r > 0)
        {
            $fl = false;
            continue;
        }
        $fl = true;
    }
    return true;
}
  
// Driver code
$N = 15;
$K = 8;
result($N, $K);
  
?>

Output: Yes

Leave a Comment

Your email address will not be published. Required fields are marked *