Q:

Write a PHP program to check if the sum of all 5' in the array exactly 15 in a given array of integers

0

Write a PHP program to check if the sum of all 5' in the array exactly 15 in a given array of integers

All Answers

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

<?php
function test($nums)
 { 

    $sum = 0;

    for ($i = 0; $i < sizeof($nums); $i++)
       {
             if ($nums[$i] == 5)
                  $sum += 5;
        }

            return $sum == 15;
 }   

var_dump(test([1, 5, 6, 9, 10, 17]));
var_dump(test([1, 5, 5, 5, 10, 17]));
var_dump(test([1, 1, 5, 5, 5, 5]));

Sample Output:

bool(false)
bool(true)
bool(false)

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