Must be done with recursion, no iterative solutions
Newtons method for approximating square roots.
The next iteration is the average of the previous iteration and the ratio of the number in question to the previous iteration
.x_i = ( x_(i-1) + number/x_(i-1) ) / 2
if i is 0 the approximation is half the number.
Pre: number & interations are non-negative integers
Post: return an approximation of sqrt(number) , the return value is double
double newton(size_t number, size_t iterations){}
#include<iostream>
#include<cmath>
using namespace std;
double root(double x, double e);
int main() {
double x,e;
cout<<"Enter a number : ";
cin>>x;
cout<<"Enter which root is to be found out ";
cin>>e;
cout << root(x,e);
return 0;
}
double root(double x, double e)
{
double a = x;
if (abs(a*a - x) <= e)
{
return a;
}
else
{
return root((a*a + x) /(2*a),e);
}
}
This is a recursive cpp code for finding square root of a number using newton's formula.
Get Answers For Free
Most questions answered within 1 hours.