Recursion formula keeps returning stack overflow

Viewed 54

I've ran the below code and I think it's correct. However, it just keeps returning stack overflow. When I run it in debug mode, I noticed somehow within the function x%y returns y instead of the remainder which is suppose to be 0. Can someone please help and see why this is?

public class test
{
    public static void main (String [] args)
    {
        System.out.println(gcd(50,10));
    }

    static double gcd(double x, double y)
    {
        if (x > y)
        {
            return gcd(y, x);
        }
        else if (y <= x && x%y == 0)
        {
            return y;
        }
        else
        {
            return gcd(y, x%y);
        }
    }
}
1 Answers
Related