w3resource

PHP Exercises: Compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum

PHP Basic Algorithm: Exercise-55 with Solution

Write a PHP program to compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes three parameters and returns a value based on certain conditions
function test($x, $y, $z)
{
    // Check if $x is equal to 13; if true, return 0
    if ($x == 13) return 0;
    
    // Check if $y is equal to 13; if true, return $x
    if ($y == 13) return $x;
    
    // Check if $z is equal to 13; if true, return the sum of $x and $y
    if ($z == 13) return $x + $y;
    
    // If none of the above conditions are met, return the sum of $x, $y, and $z
    return $x + $y + $z;
}

// Test the 'test' function with different input values and display the results
echo (test(4, 5, 7))."\n";
echo (test(7, 4, 12))."\n";
echo (test(10, 13, 12))."\n";
echo (test(13, 12, 18))."\n";
?>

Sample Output:

16
23
10
0

Flowchart:

Flowchart: Compute the sum of the three integers. If one of the values is 13 then do not count it and its right towards the sum.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to compute the sum of three given integers. If the two values are same return the third value.
Next: Write a PHP program to compute the sum of the three given integers. However, if any of the values is in the range 10..20 inclusive then that value counts as 0, except 13 and 17.

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.