Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Master #618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions Sqrt(X) Using binarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#include <bits/stdc++.h>
using namespace std;

int binarySearch(int num){

long long int beg=0,end=num;
long long int mid=beg+(end-beg)/2;
long long int ans=-1;

while(beg<=end){

if(mid*mid==num)
return mid;

else if(mid*mid<num){
ans=mid;
beg=mid+1;
}

else
end=mid-1;

mid=beg+(end-beg)/2;
}

return ans;
}

//Code by [Puneetpoter](https://github.com/Puneetpotter)

int main() {
long long int x;
cin>>x;
printf("The square root of %d calculated using binarySearch function is : ",x);
cout<<binarySearch(x);

return 0;
}