w3resource

PHP Exercises: Find the larger from two given integers

PHP Basic Algorithm: Exercise-51 with Solution

Write a PHP program to find the larger from two given integers. However if the two integers have the same remainder when divided by 7, then the return the smaller integer. If the two integers are the same, return 0.

Sample Solution:

PHP Code :

<?php
// Define a function named 'test' that compares two numbers and returns the result
function test($x, $y)
{
    // Check if $x is equal to $y
    if ($x == $y)
    {
        // Return 0 if $x is equal to $y
        return 0;
    }
    // Check if $x is not equal to $y and satisfies another condition
    else if (($x % 7 == $y % 7 && $x < $y) || $x > $y)
    {
        // Return $x if the condition is met
        return $x;
    }
    // If none of the above conditions are met
    else
    {
        // Return $y
        return $y;
    }
}

// Test the 'test' function with different input values and display the results
echo test(11, 21)."\n";
echo test(11, 20)."\n";
echo test(10, 10)."\n";
?>

Sample Output:

21
20
0

Flowchart:

Flowchart: Find the larger from two given integers.

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: 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.
Next: Write a PHP program to check two given integers, each in the range 10..99. Return true if a digit appears in both numbers, such as the 3 in 13 and 33.

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.