'use recursion to find the greatest common divisor in C

here is my code, when the input is 21,15 I got the output is 0. what I expected is 3. the return value of the function divisor seems return a wrong value.

#include<stdio.h>
int divisor(int a, int b){
    //when b is 0, got the GCD
    if(b==0){
        printf("when b is 0, a=%d\n", a);  
        return a;
    }
    else{
        //recursion
        printf("the input a=%d,b=%d\n", b, a%b);
        divisor(b, a%b);
    }
    // return res;
}

int main(void){
    int a, b;
    scanf("%d,%d", &a, &b);
    int r = divisor(a, b);
    printf("%d", r);
    return 0;
}


Solution 1:[1]

You can try this, but it will only work for positive integers.

int divisor(int a, int b){
    if (a * b == 0){
        return a+b;
    }

    return divisor(b % a, a % b);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1