/* June 23, 1998 */ /* program to find the greatest common divisor */ #include #include /* provides function abs */ /*fucntion prototype */ void scan_fraction(int *nump, int *denomp); main () { int n1, d1; int n2, d2; int q; int p; double r; int denom; int numer; int gcd; /* Get 2 fractions */ scan_fraction(&n1, &d1); scan_fraction(&n2, &d2); /* Computes numerator */ numer = n1 * d2 + n2 * d1; /* Finds a common deminator */ denom = d1 * d2; /* assign values */ q = abs(numer); p = abs(denom); r = q%p; while (r != 0) { q = p; p = r; r = q%p; } gcd = p; /* display results */ printf("The greatest common divisor, gcd, is %d\n", gcd); } /* function definition */ void scan_fraction(int *nump, int *denomp) { char slash; int status; int error; char discard; do { /* no errors detected yet */ error= 0; /* get a fraction from the user */ printf("Enter a common fraction as two integers separated "); printf("by a slash> "); status = scanf("%d %c%d", nump, &slash, denomp); /* validate the fraction */ if (status < 3) { error = 1; printf("Invalid - please read directions carefully\n"); } else if (slash != '/') { error = 1; printf("Invalid - separate numerator and denominator"); printf("by a slash (/) \n"); } else if (*denomp <= 0) { error = 1; printf("Invalid - denominator must be positive\n"); } /* discard extra input characters */ do { scanf("%c", &discard); } while (discard != '\n'); } while (error); }