/* Create a program that uses a recursive function to estimate the
square root of a number.
input: number user wants square root of
output: Square root of number and number itself
processing: nextguess = .5(lastguess-number/lastguess)
check that value is greater than 0 using function
*/
#include <iostream>
usingnamespace std;
int ifzero(double a);
int rooter (double y, double last,double);
int main ()
{
double number,y,root,fin=1;
cout<<"Enter the number of which you want to calculate the square root.";
cin>>number;
y = ifzero(number);
root = rooter(y,fin,1);
cout<<"The square root of "<<y<< " is approximately "
<< root<< ".";
system ("pause");
return 0;
}
int ifzero(double a)
{
while(a>0)
{
return a;
}
while(a<=0)
{
cout<<"Enter the number of which you want to calculate the square root.";
cin>>a;
}
}
int rooter (double numb, double final, double difference)
{
double next,number;
if(difference < .000001)
{
next=numb;
}
else
{
next = 0.5*(final - numb/final);
difference = abs (next - final);
final = next;
next = rooter (next,final,difference);
}
return next;
}