home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
nestfor1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-22
|
705b
|
32 lines
// 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;
}