/* compute an arithmetic serices in two ways */ #include int main ( ) { int n = 0; double term = 100000.0; double oldSum = 0.0; double sum = oldSum + term; printf ("adding largest to smallest terms\n"); while (oldSum != sum) { printf ("n: %3d; oldSum:%27.16lf; term:%27.16lf sum: %27.16lf \n", n, oldSum, term, sum); n++; oldSum = sum; term /= 10.0; sum = oldSum + term; } printf ("adding the last term %27.16lf, ", term); printf ("the loop finished in %d iterations with sum = %27.16lf\n\n", n, sum); // start with last term utilezed in previous computation int i; int numTerms = n; oldSum = 0.0; sum = oldSum + term; printf ("adding from smallest to largest terms\n"); for (i = 0; i < n; i++) { printf ("i: %3d; oldSum:%27.16lf; term:%27.16lf sum: %27.16lf \n", i, oldSum, term, sum); oldSum = sum; term *= 10.0; sum = oldSum + term; } printf ("adding the last term %27.16lf, ", term); printf ("loop finished in %d iterations with sum = %27.16lf\n\n", i, sum); return 0; }