training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

gcd.c (400B)


      1 /* Exercise 5.29 */
      2 
      3 int gcd(int, int);
      4 
      5 int main()
      6 {
      7    int num1, num2;
      8 
      9    printf("Give me two numbers: ");
     10    scanf("%d%d", &num1, &num2);
     11 
     12    printf("GCD: %d\n", gcd(num1, num2));
     13 
     14    return 0;
     15 } /* E0F main */
     16 
     17 /* return GCD between two numbers */
     18 int gcd(int x, int y)
     19 {
     20    int i;
     21 
     22    for(i = x; i >= 2; i--) {
     23       if( !(x % i) && !(y % i) )
     24 	 return i;
     25    }
     26    return 1;
     27 
     28 } /* eof gcd() */
     29