Abhishek Mishra Problem- 3(Square root)
#include <bits/stdc++.h>
using namespace std;
float squareRoot(int n, int pre)
{
int start = 0, end = n;
int mid;
float ans;
while (start <= end) {
mid = (start + end) / 2;
if (mid * mid == n) {
ans = mid;
break;
}
if (mid * mid < n) {
start = mid + 1;
ans = mid;
}
else {
end = mid - 1;
}
}
float increment = 0.1;
for (int i = 0; i < pre; i++) {
while (ans * ans <= n) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10;
}
return ans;
}
int main()
{
int num;
cout<<"Enter the Number" << endl;
cin>>num;
int precision;
cout<<"enter upto which decimal position you want" << endl;
cin>>precision;
cout << squareRoot(num, precision) << endl;
return 0;
}