Square Root Iteration Problem

Associate
Joined
7 Aug 2008
Posts
302
Hey lads.

Basically am going a bit insanse so am kinda hoping someone can help me out. Basically I need to create a program in C# that allows the user to enter a number that they want to find the square root off and then enter there approximation.

From this the following formula is used. new = (old + x/old) / 2. So for example if I wanted to find the square root of 25 and I put in an approximation off 7 then new would be new = (7 + 25/7) / 2. Therefore new would then = 5.2857. This would then be used for the next iteration - new = (5.2857 + 25/5.2857) /2.

This process then needs to be repeated until the number is within ±0.00000005 of the actual answer which I know how to do. Each iteration needs to be wrote to a text box. I no its going to be a Do/While loop with the While condidtion being to stop when the number is within ± 0.00000005 of the actual Square Root. So it would be 5 in this case. I just don't know how to do this.

Chears to anyone who can help.
 
namespace AssignmentTwo_QuestionTwo
{
public partial class Form1 : Form
{
double sqrtnumb;
double old;
double new1;
double answer;

public Form1()
{
InitializeComponent();
}

private void btnapprox_Click(object sender, EventArgs e)
{
sqrtnumb = Convert.ToDouble(txtsquareroot.Text);
answer = Math.Sqrt(sqrtnumb);
old = Convert.ToDouble(txtapprox.Text);
do
{
new1 = (old + sqrtnumb / old) / 2;
txtiterations.Text = String.Format("The answer is {0} ", answer);
txtiterations.Text += Environment.NewLine;
txtiterations.Text += String.Format("The next iteration is {0} ", new1);
txtiterations.Text += Environment.NewLine;
}
while ((new1 - old / new1) > 0.0000005);
{
old = new1;
new1 = (old + sqrtnumb / old) / 2;
}
}
}
}

What I've got so far which just crashes ahha!
 
Back
Top Bottom