int gcd(int a, int b);
int gcd_recurse(int a, int b);
int main()
{
printf("\nGCD(%2d,%2d) = [%d]", 6,4, gcd(6,4));
printf("\nGCD(%2d,%2d) = [%d]", 6,4, gcd_recurse(6,4));
return(0);
}
// Iterative algorithm
int gcd(int a, int b)
{
int temp;
while(b)
{
temp = a % b;
a = b;
b = temp;
}
return(a);
}
// Recursive algorithm
int gcd_recurse(int a, int b)
{
int temp;
temp = a % b;
if (temp == 0)
{
return(b);
}
else
{
return(gcd_recurse(b, temp));
}
}
No comments:
Post a Comment