C++ Program to Find the Square Root














































C++ Program to Find the Square Root



#include<iostream>
using namespace std;   
int Sqrt(int x) 
{
  if (x == 0 || x == 1) 
  return x; 
  int start = 1, finish = x, ans; 
  while (start <= finish) 
  {  
     int mid = (start + finish) / 2; 

     // If x is a perfect square 
     if (mid*mid == x) 
     return mid; 

     // If x is not a perfect square
     if (mid*mid < x) 
     { 
start = mid + 1; 
ans = mid; 
          }
     // If mid*mid is greater than x  
     else 
end = mid-1;  
   } 
return ans; 
int main() 
{  
int x = 13; 
cout << Sqrt(x) << endl; 
return 0;