Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions C++/Program-20/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## To find the square root of a number upto "p" no of places


Here we take input of that number "num" and the number "p"
45 changes: 45 additions & 0 deletions C++/Program-20/program.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include<bits/stdc++.h>
using namespace std;


float square_root(int num,int p) {
int s=0;
int e=num;
float ans=-1;
while(s<=e) {
int mid=s+e>>1; // (s+e)/2
if(mid*mid==num) {
return mid;
}
else if(mid*mid>=num) {
e=mid-1;

}
else { //as 7*7 is vey close to 50
s=mid+1;
ans=mid;
}
}

//for floating part
//brute force
float inc=0.1;
for(int i=1;i<=p;i++) {
while(ans*ans<=num) {
ans=ans+inc;
}
//when this blows up
ans=ans-inc;//comeback 1 step
inc/=10;
}
return ans;

}

int main() {
int num,p;
cin>>num;///the input number of which you want to find the square root
cin>>p;//find the square root upto "p" no of places
cout<<square_root(num,p)<<endl;
return 0;
}