Find root of a number using Newton 's method | C ++ implementation of the approach ; Function to return the square root of a number using Newtons method ; Assuming the sqrt of n as n only ; The closed guess will be stored in the root ; To count the number of iterations ; Calculate more closed x ; Check for closeness ; Update root ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double squareRoot ( double n , float l ) { double x = n ; double root ; int count = 0 ; while ( 1 ) { count ++ ; root = 0.5 * ( x + ( n / x ) ) ; if ( abs ( root - x ) < l ) break ; x = root ; } return root ; } int main ( ) { double n = 327 ; float l = 0.00001 ; cout << squareRoot ( n , l ) ; return 0 ; }