Your Ad Here

Wednesday, February 25, 2009

Write C++ programs that use both recursive and non-recursive functions to find the GCD of two given integers.

a). using Recursive function

int gcd (int, int); //func. declaration.
void main( )
{
int a, b, res;
clrscr( );
cout<<"Enter the two integer values:";
cin>> a >>b;
res= gcd(a, b); // calling function.
cout<<" GCD is:" << 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
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

1 comment: