find gcd-Greatest common divisor in C

htt‮//:sp‬www.lautturi.com
find gcd-Greatest common divisor in C

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
Created Time:2017-08-28 18:43:13  Author:lautturi