bags
New member
- Joined
- Mar 26, 2020
- Messages
- 4
- Programming Experience
- Beginner
Hello guys. I am trying to create a program to estimate the square root of a number, without using the built-in Math. functions. I have a recipe that must be followed, I think you can understand it in the loop. But for some reason it appears that the while loop is getting exited before I get the right answer? The answer needs to be precise up to 0.0001 decimals to the real square root.
PS. I first tried without the bool but did not change anything.
PS. I first tried without the bool but did not change anything.
C#:
static void Main(string[] args)
{
double est;
double n1 = 0.0001;
double estX;
double en = 1;
double to = 2;
bool status = false;
start:
Console.Write("Provide a number: ");
double x = Convert.ToDouble(Console.ReadLine());
if (x >= 1)
{
est = (en + x) / to; // first estimate
estX = (est * est);
while (status == false)
{
if (((x - n1) < estX) && (estX > (x + n1)))
status = true;
else if (estX > x)
{
est = (en + est) / to;
status = false;
}
else if (estX < x)
{
est = (est + x) / to;
status = false;
}
estX = (est * est);
}
Console.WriteLine("\nRoot of " + x + " = " + est);
Console.WriteLine("\nReal Answer: " + Math.Sqrt(x));
Console.ReadKey();
}
else
{
Console.WriteLine("\nerror 404");
Console.ReadKey();
Console.Clear();
goto start;
}
}