Program in C++ please and thank you.
Compute the square root of a number.
The input is a positive integer.
The output is the program’s estimate of the square root using Newton’s method of successive approximations.
For example, the square root estimate of 5 is 2 or square root estimate 16 is 4.
Expert Answer
#include <iostream>
#include <cmath>
using namespace std;
double squareRoot(double n);
int main()
{
double val;
cout << “Enter the number to find square root”<<endl;
cin>>val;
do{
if(val<=0){
cout<<“Ivalid number. Enter a positive number”<<endl;
cin>>val;
}
if(val>0)
cout<<“The square root estimate of “<<val<<” is ” <<round(squareRoot(val))<<endl;//– call squareRoot function to find square root using newton’s
}while(val<=0);
return 0;
}
double squareRoot(double n){
double accur = 0.001;
double result, min=1, max=1;
if(n<1)
min=n;
else
max=n;
while((max-min)>accur){
result = (min+max)/2;
if(result*result > n)
max=result;
else
min=result;
}
return (min+max)/2;
}
Sample Output: