/********************************************* * Geatest common divisor and Smallest common * * multiple of several integer numbers * * ------------------------------------------ * * Ref.: "Mathématiques en Turbo-Pascal By * * M. Ducamp and A. Reverchon (2), * * Eyrolles, Paris, 1988" [BIBLI 05]. * * ------------------------------------------ * * Sample run: * * * * GCD and SCM of integer numbers: * * * * First number: 9936 * * Next number : 414 * * Next number : 3174 * * Next number : 0 * * * * GCD = 138 * * SCM = 228528 * * * * C++ version by J-P Moreau * * (www.jpmoreau.fr) * *********************************************/ #include #include float x,pg,pp; float gcd(float a, float b) { float x,r; a=(float) int(fabs(a)); b=(float) int(fabs(b)); if (a>1e10 || b>1e10) return 1; if (a==0 || b==0) return 1; if (a1e-10); return a; } void main() { printf("\n GCD and SCM of integer numbers:\n\n"); printf(" First number: "); scanf("%f",&pg); pp=pg; do { printf(" Next number : "); scanf("%f",&x); if (x>0) { pg=gcd(pg,x); pp=pp*x/gcd(pp,x); } } while (x!=0); printf("\n GCD = %12.0f\n",pg); printf(" SCM = %12.0f\n\n",pp); } // end of file gcd.cpp