# C lang Program

How can I write a c lang program to find GCD (based on Euclidean algorithm) without remainder variable i.e. with the help of only variable ‘a’ and ‘b’.

Just like the code attached below, I’ve to create same program but using only two variables and not with r.

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

int main() {
int a,b,r;
a=15;
b=10;

while (b!=0) {

r=a%b;
a=b;
b=r;
}

printf ("The GCD of given numbers is %d", a);

return 0;
}
``````

Can anyone help on this?

Why would you want to do that? It makes the code more complicated than using `a`, `b`, and `r`.

#include <stdio.h> int main() { int a,b,r; a=160; b=132; while (b!=0) { a = a % b; if (a == 0) { a = b; break; } b = b % a; } printf ("The GCD of given numbers is %d", a); return 0; }

I recommend using a recursive function to get the GCD if you only want to have an `a` and `b`.

``````// asssuming a > b > 0
int gcd(int a, int b) {
if (b <= 0) {
return a;
}
return gcd(b, a % b);
}
``````
#include <stdio.h> // asssuming a > b > 0 int gcd(int a, int b) { if (b <= 0) { return a; } return gcd(b, a % b); } int main() { int a = 160; int b = 132; printf ("The GCD of given numbers is %d", gcd(a, b)); return 0; }

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.