Binary search Find the square root using binary search

problem- if i am giving precision value as 10 and any number then it is not giving output?

PROGRAM
///square root of a number using binary search
#include
using namespace std;
float squareroot(int number,int precision)
{
int start = 0,ends = number;
float ans = 0;
while(start <= ends)
{
int mid = (start + ends)/2;
if(midmid == number)
{
ans = mid;
break;
}
else if(mid
mid < number)
{
ans = mid;
start = mid + 1;
}
else
ends = mid - 1;
}
float increment = 0.1;
for(int i=0;i<precision;i++)
{
while((ans*ans) <= number)
{
ans += increment;
}
ans = ans - increment;
increment = increment/10;
}
return ans;
}
int main()
{
int number;
cin>>number;
int precision;
cin>>precision;

cout<<squareroot(number,precision)<<endl;
return 0;

}

Hey, can you copy your code on ide, save it and share that link here, as the code you have copied here is having many compilation errors like ‘midmid’ was not declared anywhere.

problem- if i am giving precision value as 10 and any number then it is not giving output?

https://ide.codingblocks.com/s/52007

Hey, that’s because you are using int and float as data types so float can’t take a value with that much precision so use long long int and double, it will work fine.

OK sir thanks
But sir in Winning CB scholarship problem still i am having doubt of using Binary search.I got that we are using because it will take less time but i did not get the concept of using means requirement and how it is working to get the answer??

sir now it is working fine but only showing the output till 5 places after decimal if i am giving precision more than 5

https://ide.codingblocks.com/s/52018

Hey Vijay, that’s because you are getting the exact answer at that value, otherwise double can have 15-18 digits of precision.