3 The greatest common divisor of integers x and y is the lar

3. The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a c program for recursive function gcd() that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: if y is equal to 0, then gcd (x, y) is x; otherwise gcd (x, y) is gcd (y, x%y) where % is the remainder operator.

Solution

/*GCD RECURSIVE */

# include < stdio.h >

# include < conio.h >

void main( )

{

    int m,n,z;

    clrscr ( ) ;

    printf ( \" enter the values \" );

   scanf ( \" %d %d \",&m ,&n );

    z=gcd ( m,n ) ;

    printf ( \" %d \" , z );

    getch ( );

}

int gcd ( int a , int b )

{

   int d ;

   d = a%b;

   if ( d ! = 0 )

   gcd ( b , d ) ;

else

   return ( b ) ;

}

OUTPUT -

   enter the values : 12 4

    4

   enter the values : 15 3

    3

3. The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a c program for recursive function gcd() that

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site