PHP Exercises: Check three given integers and return true if one of them is 20 or more less than one of the others
50. Check if One Integer is 20 or More Less Than Another
Write a PHP program to check three given integers and return true if one of them is 20 or more less than one of the others.
Sample Solution:
PHP Code :
<?php
// Define a function that checks if the absolute difference between two numbers is greater than or equal to 20
function test($x, $y, $z)
{
// Check if the absolute difference between $x and $y is greater than or equal to 20,
// or if the absolute difference between $x and $z is greater than or equal to 20,
// or if the absolute difference between $y and $z is greater than or equal to 20
return abs($x - $y) >= 20 || abs($x - $z) >= 20 || abs($y - $z) >= 20;
}
// Test the function with different sets of numbers
var_dump(test(11, 21, 31))."\n";
var_dump(test(11, 22, 31))."\n";
var_dump(test(10, 20, 15))."\n";
?>
Explanation:
- Function Definition:
- The function test checks if the absolute difference between any two of the three numbers $x, $y, or $z is greater than or equal to 20.
- Condition Checked:
- The function calculates the absolute difference between:
- $x and $y
- $x and $z
- $y and $z
- It then checks if any of these differences are >= 20.
Output:
bool(true) bool(true) bool(false)
Visual Presentation:

Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to check among three integers if one is at least 20 less than any other, returning a boolean result.
- Write a PHP function to compare three numbers and determine if the difference between any two meets or exceeds 20.
- Write a PHP program to validate three integers and use absolute differences to check if one is substantially smaller by at least 20.
- Write a PHP script to implement a set of conditional checks among three integers to decide if one value is at least 20 lower than another.
Go to:
PREV : Same Rightmost Digit Among Integers.
NEXT : Larger with Remainder Rule.
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.