# euclids algorithm to find GCD with counter not getting right output

• 09-25-2011
dsured
euclids algorithm to find GCD with counter not getting right output
Code:

```#include <stdio.h> int euclid(int a,int b, int c) {         if(b==0){                 c++;         return a;                         }         else {                 c++;         return euclid(b,a%b, c);         } } int main() {         int n1,n2;         int counter;         printf("Enter two numbers to find its GCD:");         scanf("%d %d",&n1,&n2);         printf("The GCD of %d and %d is %d - counter = %d",n1,n2,euclid(n1,n2, counter),counter);         return 0; }```
Here is my code, when I run it I get this

Enter two numbers to find its GCD:20
5
The GCD of 20 and 5 is 5 - counter = 0
• 09-25-2011
CommonTater
Well, for starts the greatest common denominator of 20 and 5 is 5... so that answer is correct.

Your counter is 0 because you are not incrementing it. Passing it into a function sends in a *copy* of counter. When you increment c inside the function it has no effect on anything outside the function. If you want to increment a variable in main, from within a function, you need to pass in a pointer...