Square root of a number without using sqrt ( ) function | C ++ implementation of the approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } void findSqrt ( double n ) { double i = 1 ; bool found = false ; while ( ! found ) { if ( i * i == n ) { cout << fixed << setprecision ( 0 ) << i ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; cout << fixed << setprecision ( 5 ) << res ; found = true ; } i ++ ; } } int main ( ) { double n = 3 ; findSqrt ( n ) ; return 0 ; }