# dont really understand prob,plz help

This is a discussion on dont really understand prob,plz help within the C Programming forums, part of the General Programming Boards category; I'm new to C programming I'll try to write the program, but I don't even know what the program is ...

1. ## dont really understand prob,plz help

I'm new to C programming
I'll try to write the program, but I don't even know what the program is asking me to write

The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd taht 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 remainder operator.

heres my attempt at this program, not a very good 1 cuz i barely understand the program i need to write

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

long gcd (long gcd)

int main()
{
int x,y,i;

for (i=0; i<=100; i++)
{
printf("Enter 1st integer\n");
scanf("%d",&x);

printf("Enter 2nd integer\n");
scanf("%d",&y);
}

return 0;
}

long gcd (long gcd)
{
if (y==0)
{
printf("gcd(x,y) is %d\n", x);
}
else
{
printf("gcd(x,y) is gcd(%d,%d %% %d\n", y,x,y);
}

return 0;
}```

2. Do you know what a recursive function is? It's a function that calls itself. Does that help you on your way at all?

3. heres the revised code

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

long gcd (int,int);

int main()
{
int x,y,i;

for (i=0; i<=100; i++)
{
printf("Enter 1st integer\n");
scanf("%d",&x);

printf("Enter 2nd integer\n");
scanf("%d",&y);
}

return 0;
}

long gcd (int x,int y)
{
if (y)
{
return gcd(y, x%y);
}

else
{
return x;
}

return 0;
}```
prblem im having is that it keeps on asking me to enter 1st integer and 2nd integers

4. Originally Posted by strider496
prblem im having is that it keeps on asking me to enter 1st integer and 2nd integers
That's what your program does. If you want it to call your function, write the code to call your function.

5. man.. the problem with u is that you know the problem but ur not making an effort as the why that problem is coming up..

U know that it keeps asking you to enter the numbers.. but u fail to notice or understand the reason as why you have put that "for" loop for..

You know that u are suppose to call a function with in a function.. you write a function.. too.. but have you called that function.. from you main..

the above mentioned problem. just are pointers.. buddy.. i would like you to solve the problem urself.. rather than me typing the code for you..!!!!

The fun is not in codeing.. but in debugging your own code.. finding as to why ur code doesn't work.. !!!!

I don't want to spoil you fun.. bye..

6. revised

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

int gcd(int a, int b);

int main()
{
int x, y, z;
int gcd(int, int);

printf("Enter 1st integer\n");
scanf("%d",&x);

printf("Enter 2nd integer\n");
scanf("%d",&y);

if(y==0)
{
z = gcd(x, y);
printf("gcd(%d,%d) is %d\n" x,y,z ;}
else
printf("gcd(%d,%d is gcd(%d, %d %% %d)\n",x,y,x,x%y);
}

int gcd(int a, int b)
{
while (a != b){
if (a > b)
a -= b;
else
b -= a;
}
return a;
}```

7. What did you do with the recursiveness?!

8. lol @ recursiveness.

Anyways, your revisions seem to be taking you further from what you need, especially the last one where you took out all of the recursion.

9. thx guys got my programs to run

10. Well your problem asked you to use recursion to solve the problem - you nearly had it in your 2nd revision but you didn't call the gcd function which defeated the purpose of having a gcd function. In the last revision you called the gcd function but the function itself wasn't implemented via recursion. So if you combine what you did right in the 2nd/3rd revision you should get something like this:

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

int gcd(int x, int y);

int main(int argc, char **argv)
{
int x, y, z;

printf("Enter 2 integers: ");
scanf("%d %d",&x,&y);

printf("The gcd of (%d,%d) is %d\n",x,y,gcd(x,y));

return 0;
}

int gcd(int x, int y)
{
if (y == 0)
{
return x;
} else {
return gcd(y,x%y);
}
}```

11. Orion,

Another c programming student here... At the bottom of your code above, why did you place the brackets around "return x;" and "return gcd..."? I compiled with and without those two brackets and got the same result. Just wanna know if I'm missing something good here.

Thanks...

12. Full bracketing is a style that helps avoid bugs.
http://www.psgd.org/paul/docs/cstyle/cstyle09.htm