Find out the GCD(Greatest Common Divisor) of the given two numbers in PHP.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    Find out the GCD(Greatest Common Divisor) of the given two numbers in PHP.

    It's a manual process to find out the GCD of given two numbers that we've learned in our childhood in general mathematics on standard 2/3.






    function getGCD($num1, $num2) {

    if ($num1 == 0 || $num2 == 0)
    return false;

    $divisor = $num1;
    $dividend = $num2;

    if ($num1 > $num2) {
    $divisor = $num2;
    $dividend = $num1;
    }

    $remainder = $dividend % $divisor;
    $quotient = $dividend / $divisor;

    if ($remainder !== 0) {
    $result = $remainder;
    } else {
    $result = $quotient;
    }

    return $result;
    }

    print_r(getGCD(12, 16)); // Output: 4









    More...
Working...