I have to write a program that returns the square root of any number that is inputted. My method runs fine for any number that has a perfect square (like 25 or 100) but if I use any number that doesn't (3 or 10) I get a stack overflow error. I was hoping some one could tell me what I did wrong and how I could fix it.
public boolean sqrRootRecursive(int number, double approx, double tol)
{
if( (Math.abs((Math.pow(approx,2) - number))<= tol))
{
System.out.println("The root is " +approx);
}else if (Math.abs((Math.pow(approx,2)-number))>tol)
{
sqrRootRecursive(number, ((Math.pow(approx,2)+number)/(2*approx)), tol);
}
return true;
Thanks for the help!