Program to calculate the square root of a number using a binary search approach.

Rajat Jadam
1 min readFeb 9, 2020

--

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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response