gcd-Greatest common divisor
/* Example: get the greatest common divisor in C language */ #include <stdio.h> #include <stdlib.h> #include <string.h> int gcd_iter(int u, int v) { if (u < 0) u = -u; if (v < 0) v = -v; if (v) while ((u %= v) && (v %= u)); return (u + v); } int main(int argc, char const *argv[]) { int a = 18; int b = 24; printf("The gcd of %d and %d is: %d",a,b, gcd_iter(a, b)); }
output:
18 and 24 , gcd is: 6