home *** CD-ROM | disk | FTP | other *** search
- // Program demonstrates the do-while loop
-
- #include <stdio.h>
-
- const double TOLERANCE = 1.0e-7;
- const int MIN_NUM = 1;
- const int MAX_NUM = 10;
-
- double abs(double x)
- {
- return (x >= 0) ? x : -x;
- }
-
- main()
- {
- double x, sqrt;
-
- printf(" X Sqrt(X)\n");
- printf("_____________________\n\n");
- // outer loop
- for (int i = MIN_NUM; i <= MAX_NUM; i++) {
- x = (double)i;
- sqrt = x /2;
- // inner loop
- do {
- sqrt = (sqrt + x / sqrt) / 2;
- } while (abs(sqrt * sqrt - x) > TOLERANCE);
- printf("%4.1f %8.6lf\n", x, sqrt);
- }
- return 0;
- }
-