- #1
- 4
- 0
I'm new to programming. my assignment wants me to create a program using c++ that can find the square root of any number using Newtons Method.
The user has to enter a number (y) and the program has to calculate the root and has to show each iteration. the initial guess (x0) has to be y/4
the iterations have to stop when the initial guess (x0) and the new guess (x1) are within 0.0001 of each other
Newtons method equation: x1 = (x0 + (y/x0))/2
the problem is that i keep getting a infinite loop showing the first x0 and x1 values
I think the loop isn't updating the x1 and x0 values after each iteration
how do i get the loop to update the x1 and x0 values
any help would be appreciated
here what i got for my code so far:
# include <iostream>
# include <math.h>
using namespace std;
int main ()
{
double y = 0, x1 = 0, x0 = 0;
cout << "enter number" << endl;
cin >> y;
x0 = ( y/4);
while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))
{
x1 = ((x0 + (y/x0))/2);
x0 == x1;
cout << "x0=" << x0 << "x1=" << x1 << endl;
}
return 0;
}
The user has to enter a number (y) and the program has to calculate the root and has to show each iteration. the initial guess (x0) has to be y/4
the iterations have to stop when the initial guess (x0) and the new guess (x1) are within 0.0001 of each other
Newtons method equation: x1 = (x0 + (y/x0))/2
the problem is that i keep getting a infinite loop showing the first x0 and x1 values
I think the loop isn't updating the x1 and x0 values after each iteration
how do i get the loop to update the x1 and x0 values
any help would be appreciated
here what i got for my code so far:
# include <iostream>
# include <math.h>
using namespace std;
int main ()
{
double y = 0, x1 = 0, x0 = 0;
cout << "enter number" << endl;
cin >> y;
x0 = ( y/4);
while (!( x1 - x0 <= 0.0001 && x1 - x0 >= -0.0001))
{
x1 = ((x0 + (y/x0))/2);
x0 == x1;
cout << "x0=" << x0 << "x1=" << x1 << endl;
}
return 0;
}