GCD of two numbers using function in C

In this program, you will take two numbers from the user and finding the GCD of two numbers. GCD is also called HCF (Highest Common Factor).

For example, the GCD of two numbers, 8 and 12 are 4 because factors for 8 are 1,2,4,8 and factor for 12 are 1,2,3,4,6,12, largest comman factor is 4. hence GCD of 8 and 12 is 4.

int main() {
    int x, y;
    printf("Enter 2 numbers:");
    scanf("%d%d", &x, &y);
    printf("Greatest Common Divisor is %d", gcd(x, y));
    return 0;
int gcd(int x, int y) {
  if (y == 0)
    return x;
    return gcd(y, x % y);


Enter 2 numbers:12  18
Greatest Common Divisor is 6