Index « Previous Next »

Question

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;
    else
        return gcd(y, x % y);
}

Output

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