Thursday, 26 January 2012

Write a C program to find the GCD (greatest common divisor) of two given integers using Recursive function.

#include<stdio.h>
#include<conio.h>
int gcd (int, int); //func. declaration.
void main( )
{
    int a, b, res;
    clrscr( );
    printf("Enter the two integer values:");
    scanf("%d%d", &a, &b);
    res= gcd(a, b); // calling function.
    printf("\nGCD of %d and %d is: %d", a, b, res);
    getch( );
}
int gcd( int x, int y) //called function.
{
    int z;
    z=x%y;
    if(z==0)
    return y;
    gcd(y,z); //recursive function
}

No comments:

Post a Comment