Skip to main content
Removed else.
Source Link

Alternate Recursive Method

The above snippet is equivalent to this, which may better meet your preferenceCan also be written as:

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
    {
        return x;
    }
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    else
    {
        return GetGCD(y, x % y);
    }
}

Depending on your preference.

Alternate Recursive Method

The above snippet is equivalent to this, which may better meet your preference:

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
    {
        return x;
    }
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    else
    {
        return GetGCD(y, x % y);
    }
}

Can also be written as:

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
        return x;
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    return GetGCD(y, x % y);
}

Depending on your preference.

Corrected myself on scoping variables. Adjusted it so my answer is on top, crediting other answers for iterative method.
Source Link

Iterative Method

There's no need to create tmp every iteration. The variable can be instantiated outside of the loop. Variable swapping can be done in this instance.

static int GetGCD(int x, int y)
{
    int tmp;
    while (y != 0)
    {
        tmp = x % y;
        x = y;
        y = tmp;
    }
    return x;
}

Recursive Method

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
    {
        return x;
    }
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    else
    {
        return GetGCD(y, x % y);
    }
}

Iterative Method

As stated in other answers, you might use an iterative method:

static int GetGCD(int x, int y)
{
    while (y != 0)
    {
        int tmp = x % y;
        x = y;
        y = tmp;
    }
    return x;
}

Iterative Method

There's no need to create tmp every iteration. The variable can be instantiated outside of the loop. Variable swapping can be done in this instance.

static int GetGCD(int x, int y)
{
    int tmp;
    while (y != 0)
    {
        tmp = x % y;
        x = y;
        y = tmp;
    }
    return x;
}

Recursive Method

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
    {
        return x;
    }
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    else
    {
        return GetGCD(y, x % y);
    }
}

Recursive Method

static int GetGCD(int x, int y)
{
    // If y is equal to 0, return x.
    if (y == 0)
    {
        return x;
    }
    // If y is not equal to 0, recursive call with x as y and y as the remainder.
    else
    {
        return GetGCD(y, x % y);
    }
}

Iterative Method

As stated in other answers, you might use an iterative method:

static int GetGCD(int x, int y)
{
    while (y != 0)
    {
        int tmp = x % y;
        x = y;
        y = tmp;
    }
    return x;
}
Added benchmark and iterative method.
Source Link

There's no need to create tmp every iteration. The variable can be instantiated outside of the loop. Variable swapping can be done in this instance.

There's no need to create tmp every iteration. The variable can be instantiated outside of the loop.

There's no need to create tmp every iteration. The variable can be instantiated outside of the loop. Variable swapping can be done in this instance.

Added benchmark and iterative method.
Source Link
Loading
Formatted.
Source Link
Loading
added 105 characters in body
Source Link
Loading
Source Link
Loading