square root recursion problem
Nov 1, 2015 at 12:55pm UTC
I am new to C++ - pleaase bear with me! Below is a bit of code to find square roots recursively. Although it works, the answer is always 7 characters long? I can not seem to increase the accuracy of the answer by getting more decimal places?
Can anyone help please...!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include<iostream>
#include<cmath>
using namespace std;
double sqroot (double number,long double previteration);
double roottofind;
int main()
{
cout << "Input a number to find the square root: " ;
cin >> roottofind;
cout << roottofind << endl;
double firstiteration = roottofind/2;
cout << "Squareroot is: " << sqroot(roottofind,firstiteration);
cout << endl;
return 0;
}
double sqroot (double number,long double previteration)
{
long double root = (0.5 * (previteration + (number/previteration)));
if (abs (previteration - root) >= 0.00000000000001)
return sqroot(number,root);
else
return root;
}
Nov 1, 2015 at 1:12pm UTC
Thank you very much!!
Problem solved :)
Topic archived. No new replies allowed.