Square root of an integer | A C # program to find floor ( sqrt ( x ) ; Base Cases ; Do Binary Search for floor ( sqrt ( x ) ) ; If x is a perfect square ; Since we need floor , we update answer when mid * mid is smaller than x , and move closer to sqrt ( x ) ; If mid * mid is greater than x ; Driver Code
using System ; class GFG { public static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int start = 1 , end = x , ans = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid * mid == x ) return mid ; if ( mid * mid < x ) { start = mid + 1 ; ans = mid ; } else end = mid - 1 ; } return ans ; } static public void Main ( ) { int x = 11 ; Console . WriteLine ( floorSqrt ( x ) ) ; } }