C++ Code Example: greatest common divisor (recursion)

The gcd of two numbers is the greatest common divisor of these numbers, that is the greatest number by which both numbers are divisible.

#include <iostream>
using namespace std;

static int gcd(int a, int b) {
    if (a == 0) {
        return b;
    } else if (b == 0) {
        return a;
    } else if (a > b) {
        return gcd(a - b, b);
    } else {
        return gcd(a, b - a);
    }
}

int main() {
    int a = 90, b = 60;

    cout << "Greatest common divisor is: " << gcd(a, b) << endl;

    return 0;
}
Output
Greatest common divisor is: 30