Use a function to find the greatest common divisor

6-1 Use functions to find the greatest common divisor (10 points)
This problem asks to implement a simple function that calculates the greatest common divisor of two numbers.
Function interface definition:
int gcd( int x, int y );
where x and y are two positive integers, and the function gcd should return the greatest common divisor of these two numbers.
Example of the referee test procedure:

这里写代码片


#include <stdio.h>

int gcd( int x, int y );

int main()
{
    int x, y;

    scanf("%d %d", &x, &y);
    printf("%d\n", gcd(x, y));

    return 0;
}

/* Your code will be embedded here */
Input sample:
32 72
Output sample:
8

int gcd(int x,int y)
{
    int t;
    while(y)
    {
        t=x%y;
        x=y;
        y=t;
    }
    return  x;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686238&siteId=291194637