w3resource
w3resource logo

PHP Exercises: Compute the sum of values in a given array of integers except the number 17

(追記) (追記ここまで)
(追記) (追記ここまで)

108. Sum of Array Except 17

Write a PHP program to compute the sum of values in a given array of integers except the number 17. 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 a variable $sum with a value of 0
 $sum = 0;
 // Iterate through the elements of the array using a for loop
 for ($i = 0; $i < sizeof($nums); $i++) { // Check if the current element is not equal to 17, increment $sum by the value of the current element if ($nums[$i] != 17) $sum += $nums[$i]; } // Return the sum of values in the array except for the number 17 return $sum; } // Call the 'test' function with an array [1, 5, 7, 9, 10, 17] and print the result echo "Sum of values in the array of integers except the number 17: ". test([1, 5, 7, 9, 10, 17] ); ?> 

Sample Output:

Sum of values in the array of integers except the number 17: 32

Flowchart:

Flowchart: Compute the sum of values in a given array of integers except the number 17.

For more Practice: Solve these Related Problems:

  • Write a PHP script to calculate the sum of an array’s values while skipping any occurrence of the number 17.
  • Write a PHP function to iterate through an array and sum only those values that are not equal to 17.
  • Write a PHP program to use conditional logic to omit 17 from the summation of an array’s elements.
  • Write a PHP script to perform a cumulative sum on an array with a filter that excludes the number 17.

Go to:


PREV : Difference Between Largest and Smallest.
NEXT : Sum Excluding Numbers Starting with 5 Followed by 6.

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.

(追記) (追記ここまで)


(追記) (追記ここまで)
(追記) (追記ここまで)


AltStyle によって変換されたページ (->オリジナル) /