home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
dowhile1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-22
|
807b
|
46 lines
// Program demonstrates the do-while loop
#include <iostream.h>
const double TOLERANCE = 1.0e-7;
double abs(double x)
{
return (x >= 0) ? x : -x;
}
double sqroot(double x)
{
double guess = x / 2;
do {
guess = (guess + x / guess) / 2;
} while (abs(guess * guess - x) > TOLERANCE);
return guess;
}
double getNumber()
{
double x;
do {
cout << "Enter a number: ";
cin >> x;
} while (x < 0);
return x;
}
main()
{
char c;
double x, y;
do {
x = getNumber();
y = sqroot(x);
cout << "Sqrt(" << x << ") = " << y << "\n"
<< "Enter another number? (Y/N) ";
cin >> c;
cout << "\n";
} while (c == 'Y' || c == 'y');
return 0;
}