0

Hi everyone,

I have written this code which can find the square root of a positive number using newtons method. The code below describes it all. Now, I just need to know how can I show each iteration of the loop? like I wanna out each iteration until the final value is reached.

Thank you in advance

```
// A program that prints out the square root of a positive value entered by user
#include <iostream> // for cin and cout
#include <cmath> // for math calculations
using namespace std;
int main() // Block of Code
{
double value; // We use double to avoid overflow
double value1, value2;
cout <<"*******************************************" << endl;
cout << "Hey! Welcome to the simple calculator" << endl;
cout <<"*******************************************" << endl << endl << endl;
cout << "A Calculator that finds the square root of a number using Newton's method"
<< endl << endl; // using multiple endl just to make the code look better to user
cout << "Enter a positive number and we will calculate its square root: ";
cin >> value;
if (value < 0.0)// negative values are not accepted
cout << "Sorry, I'm unable to perform this caculation!"
<< endl;
else
if (value == 0.0)
cout << "square root of "
<< value
<< " is 0.00"
<< endl;
else
{
value1 = value/4.0; // initial value
value2 = (value1 + value/value1)/2;
while (fabs(value2-value1) > 0.0001)// for float absolute values
{
value1 = value2;
value2 = (value1 + value/value1)/2;
}
cout << "square root of "
<< value
<< " is " << value2 // prints out the square root of the value entered
<< endl;
}
system ("pause"); // inside only the main function and not any other one!
// because otherwise the system will pause only to show the square root of
// a positive value
}
```

*Edited
by Dani*: Fixed formatting