# Thread: NEED help with returning the GCD

1. ## NEED help with returning the GCD

umm, i need help with this, i have no idea how to make it return the greatest common divisor, this code works without returning anything, but i need to make the code to RETURN it......i gotten everything down, just donno how to return the answer....

Code:
```#include <stdio.h>
#include <stdlib.h>

int	GCD( int a,int b, int r);
int main()
{
int	r,a,b;

printf("Please enter two integers separated by a space: ");
scanf("%d %d",&a,&b);

printf("\nYou entered: %d %d ",a,b);

if(a+b>0)
{
while (b)
{
r = a % b;
a = b;
b = r;
}
r=GCD(a,b,r);
printf("\n\n\nGCD: %d\n\n",r);
}
else
{
return 0;
}

system("PAUSE");
return 0;
}

int GCD(int a,int b, int r)
{

return ;

}```

2. First off you need to put the algorithm code inside the function.
Second, a GCD function only takes two parameters. r should simply be a local variable.

3. well, i think i fixed it but its not giving me the answer correctly...for example if u put 32 and 12, the answer is supposed to be 4, but it gives it as 0

but if u "return a" it happens to work correctly, but isnt this program supposed to return gcd or r since it's being called..? why isnt it returning r correctly?

Code:
```#include <stdio.h>
#include <stdlib.h>

int	GCD( int a,int b);
int main()
{
int	r,a,b;

printf("Please enter two integers separated by a space: ");
scanf("%d %d",&a,&b);

printf("\nYou entered: %d %d ",a,b);

if(a+b>0)
{
r=GCD(a,b);
printf("\n\n\nGCD: %d\n\n",r);
}
else
{
return 0;
}

system("PAUSE");
return 0;
}

int GCD(int a,int b)
{
int r;
while (b)
{
r = a % b;
a = b;
b = r;
}

return r;
}```

4. That looks much better!
It does need to return a instead of r. Afterall b is set equal to r, and we only exit the loop when b is zero, so at that point r will be zero too.