w3resource

PHP Exercises: Check three given integers and return true if the difference between small and medium and the difference between medium and large is same

PHP Basic Algorithm: Exercise-58 with Solution

Write a PHP program to check three given integers (small, medium and large) and return true if the difference between small and medium and the difference between medium and large is same.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that takes three parameters and returns a boolean value based on conditions
function test($x, $y, $z)
{
    // Check if the sequence of numbers is in ascending order and the differences are equal
    if ($x > $y && $x > $z && $y > $z) return $x - $y == $y - $z;
    
    // Check if the sequence of numbers is in ascending order and the differences are equal
    if ($x > $y && $x > $z && $z > $y) return $x - $z == $z - $y;
    
    // Check if the sequence of numbers is in ascending order and the differences are equal
    if ($y > $x && $y > $z && $x > $z) return $y - $x == $x - $z;
    
    // Check if the sequence of numbers is in ascending order and the differences are equal
    if ($y > $x && $y > $z && $z > $x) return $y - $z == $z - $x;
    
    // Check if the sequence of numbers is in ascending order and the differences are equal
    if ($z > $x && $z > $y && $x > $y) return $z - $x == $x - $y;
    
    // If none of the above conditions are met, check if the sequence of numbers is in ascending order and the differences are equal
    return $z - $y == $y - $x;
}

// Test the 'test' function with different input values and display the results
var_dump(test(4, 5, 6));
var_dump(test(7, 12, 13));
var_dump(test(-1, 0, 1));
?>

Sample Output:

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

Flowchart:

Flowchart: Check three given integers and return true if the difference between small and medium and the difference between medium and large is same.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to check two given integers and return the value whichever value is nearest to 13 without going over. Return 0 if both numbers go over.
Next: Write a PHP program to create a new string using two given strings s1, s2, the format of the new string will be s1s2s2s1.

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.