w3resource

PHP Exercises: Compute the sum of the numbers in a given array except those numbers starting with 5 followed by atleast one 6


109. Sum Excluding Numbers Starting with 5 Followed by 6

Write a PHP program to compute the sum of the numbers in a given array except those numbers starting with 5 followed by atleast one 6. Return 0 if the given array has no integer.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes an array of numbers as a parameter
function test($nums)
 {
    // Initialize variables $sum, $inSection, and $flag with values of 0
    $sum = 0;
    $inSection = 0;
    $flag = 0;

    // Iterate through the elements of the array using a for loop
    for ($i = 0; $i < sizeof($nums); $i++)
    {
        // Set $inSection to 0 at the beginning of each iteration
        $inSection = 0;

        // Check if the current element is equal to 5
        if ($nums[$i] == 5)
        {
            // Set $inSection to 0 and $flag to 1 if true
            $inSection = 0;
            $flag = 1;
        }

        // Check if $inSection is 0 and the current element is 6
        if ($inSection == 0 && $nums[$i] == 6)
        {
            // Check if $flag is 1
            if ($flag == 1)
            {
                // Subtract 5 from $sum, set $inSection to 1
                $sum = $sum - 5;
                $inSection = 1;
            }
            
            // Set $flag to 0
            $flag = 0;
        }

        // Check if $inSection is 0
        if ($inSection == 0)
        {
            // Add the value of the current element to $sum
            $sum += $nums[$i];
        }
    }

    // Return the sum of the numbers in the array, excluding those starting with 5 followed by at least one 6
    return $sum;
 }

// Print the results for different arrays
echo "Sum of the numbers of the said array except those numbers starting with 5 followed by at least one 6:\n". test([1, 5, 7, 9, 10, 17] );
echo "\nSum of the numbers of the said array except those numbers starting with 5 followed by at least one 6:\n". test([1, 5, 6, 9, 10, 17] );
echo "\nSum of the numbers of the said array except those numbers starting with 5 followed by at least one 6:\n". test([5, 6, 7, 9, 10, 17, 1] );
echo "\nSum of the numbers of the said array except those numbers starting with 5 followed by at least one 6:\n". test([11, 9, 10, 17, 5, 6] );
?>

Sample Output:

Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
49
Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
37
Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
44
Sum of the numbers of the said array except those numbers starting with 5 followed by atleast one 6:
47

Flowchart:

Flowchart: Compute the sum of the numbers in a given array except those numbers starting with 5 followed by atleast one 6.

For more Practice: Solve these Related Problems:

  • Write a PHP script to sum elements in an array while omitting numbers that begin with 5 and have a 6 immediately following in their digit sequence.
  • Write a PHP function to convert numbers to strings and conditionally exclude those matching the pattern "56..." from the total sum.
  • Write a PHP program to apply regex matching on the string representation of numbers and skip summing those that fit the disallowed pattern.
  • Write a PHP script to iterate over an array, check each value’s digits, and add only those that do not start with 5 followed by 6.

Go to:


PREV : Sum of Array Except 17.
NEXT : Contains 5 Next to 5 Check in Array.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.