/*------------------------------------------------------------------*/ /* testmymath.c (Version 2) */ /*------------------------------------------------------------------*/ #include /*------------------------------------------------------------------*/ /* Function declarations */ /*------------------------------------------------------------------*/ int gcd(int iFirst, int iSecond); int lcm(int iFirst, int iSecond); /*------------------------------------------------------------------*/ /* Function definitions */ /*------------------------------------------------------------------*/ int main(void) /* Read two positive integers from stdin, compute the greatest common divisor and least common multiple of the two numbers, and write those two values to stdout. */ { int i1; int i2; int iGcd; int iLcm; printf("Enter the first positive integer: "); scanf("%d", &i1); printf("Enter the second positive integer: "); scanf("%d", &i2); iGcd = gcd(i1, i2); iLcm = lcm(i1, i2); printf("\n"); printf("The greatest common divisor of %d and %d is %d.\n", i1, i2, iGcd); printf("The least common multiple of %d and %d is %d.\n", i1, i2, iLcm); return 0; } /*------------------------------------------------------------------*/ int gcd(int iFirst, int iSecond) /* Return the greatest common divisor of iFirst and iSecond. */ { int iTemp; while (iSecond != 0) { iTemp = iFirst % iSecond; iFirst = iSecond; iSecond = iTemp; } return iFirst; } /*------------------------------------------------------------------*/ int lcm(int iFirst, int iSecond) /* Return the least common multiple of iFirst and iSecond. */ { return (iFirst / gcd(iFirst, iSecond)) * iSecond; }