 # Answered! Program in C++ please and thank you. Compute the square root of a number….

Program in C++ please and thank you.

Compute the square root of a number.

Don't use plagiarized sources. Get Your Custom Essay on
Answered! Program in C++ please and thank you. Compute the square root of a number….
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS \$13/PAGE

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.

Sample Code:

#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: 