Q:

Write a PHP program to count the number of two 5's are next to each other in an array of integers

0

Write a PHP program to count the number of two 5's are next to each other in an array of integers. Also count the situation where the second 5 is actually a 6

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function test($numbers)
{
          $ctr = 0;
            for ($i = 0; $i < sizeof($numbers) - 1; $i++)
            {
                if (($numbers[$i]==5) && ($numbers[$i + 1]==5) || ($numbers[$i + 1]==6)) $ctr++;
            }
            return $ctr;
 }

echo test([5, 5, 2])."\n";
echo test([5, 5, 2, 5, 5])."\n";
echo test([5, 6, 2, 9])."\n";

Sample Output:

1
2
1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now