Square root of a number without using sqrt ( ) function | Recursive function that returns square root of a number with precision upto 5 decimal places ; If mid itself is the square root , return mid ; If mul is less than n , recur second half ; Else recur first half ; Function to find the square root of n ; While the square root is not found ; If n is a perfect square ; Square root will lie in the interval i - 1 and i ; Driver code
function Square ( n , i , j ) { var mid = ( ( i + j ) / 2 ) ; var mul = mid * mid ; if ( ( mul == n ) || ( Math . abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } function findSqrt ( n ) { var i = 1 ; var found = false ; while ( ! found ) { if ( i * i == n ) { document . write ( i ) ; found = true ; } else if ( i * i > n ) { var res = Square ( n , i - 1 , i ) ; document . write ( res . toFixed ( 5 ) ) ; found = true ; } i ++ ; } } var n = 3 ; findSqrt ( n ) ;