/*--------------------------------------------------------------------*/ /* testintmath.c (Version 2) */ /* Author: Bob Dondero */ /*--------------------------------------------------------------------*/ #include #include /*--------------------------------------------------------------------*/ /* Function declarations */ /*--------------------------------------------------------------------*/ static int gcd(int iFirst, int iSecond); static 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. Return 0 if successful. */ { int i1; int i2; int iGcd; int iLcm; int iScanfReturn; printf("Enter the first positive integer:\n"); iScanfReturn = scanf("%d", &i1); if ((iScanfReturn != 1) || (i1 <= 0)) { fprintf(stderr, "Error: Not a positive integer.\n"); exit(EXIT_FAILURE); } printf("Enter the second positive integer:\n"); iScanfReturn = scanf("%d", &i2); if ((iScanfReturn != 1) || (i2 <= 0)) { fprintf(stderr, "Error: Not a positive integer.\n"); exit(EXIT_FAILURE); } iGcd = gcd(i1, i2); iLcm = lcm(i1, i2); 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; } /*--------------------------------------------------------------------*/ static int gcd(int iFirst, int iSecond) /* Return the greatest common divisor of positive integers iFirst and iSecond. */ { int iTemp; /* Use Euclid's algorithm. */ while (iSecond != 0) { iTemp = iFirst % iSecond; iFirst = iSecond; iSecond = iTemp; } return iFirst; } /*--------------------------------------------------------------------*/ static int lcm(int iFirst, int iSecond) /* Return the least common multiple of positive integers iFirst and iSecond. */ { return (iFirst / gcd(iFirst, iSecond)) * iSecond; }