Program to calculate the square root of a number using a binary search approach.
#include<iostream>
using namespace std;
int main()
{
float n;
cout<<”Enter the value of N:”;
cin>>n;
float low=0;
float high=n;
float mid;
while((high-low)>0.00001)
{
mid=(low+high)/2;
if((mid*mid)<n){
low=mid;
}
else{
high=mid;
}
}
cout<<low;
}
#NOTE:
The value inside the while condition can be adjusted to give more precision. But on increasing value, We need more computations to calculate the square root.