Advertisement
Shuva_Dev

Find Square Root of N (Binary Search)

Dec 10th, 2022 (edited)
1,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.41 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define endl "\n"
  3. using namespace std;
  4.  
  5. double find_sqrt(int n) {
  6.     double L = 0, R = n;
  7.  
  8.     while(fabs(L-R) >= 1e-4) {
  9.         double M = (L+R) / 2;
  10.         if(M*M < n) {
  11.             L = M;
  12.         } else {
  13.             R = M;
  14.         }
  15.     }
  16.     return L;
  17. }
  18.  
  19. int main() {
  20.     int n;
  21.     cin >> n;
  22.     cout << find_sqrt(n) << endl;
  23.     main();
  24.    
  25.    
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement