Index « Previous Next »


Write a program that reads two integers from keyboard and calculate the greatest common divisor (gcd) using recursive function.

Source Code

#include <stdio.h>

int gcd(int, int);

int main()
    int num1, num2;

    printf("Enter two integers: ");
    scanf("%d%d", &num1, &num2);

    printf("The greatest common divisor of %d and %d is %d", num1, num2, gcd(num1, num2));
    return 0;

int gcd(int x, int y)
    if (x % y == 0)
        return y;
        return gcd(y, x % y);


Enter two integers: 18 84
The greatest common divisor of 18 and 84 is 6