Python Program to find the square root of a number by Newton’s Method?

 Newton's Method to find Square root

Let the given number be b and let x be a rough guess of the square root of b. 

New x = 0.5 * (x + b/x)

Program Code

def newton_method(number, number_iters = 100):

    a = float(number) 

    for i in range(number_iters): 

        number = 0.5 * (number + a / number) 

    return number

a=int(input("Enter first number:"))

b=int(input("Enter second number:"))

print("Square root of first number:",newton_method(a))

print("Square root of second number:",newton_method(b))

Output

Enter first number:81
Enter second number:5
Square root of first number: 9.0
Square root of second number: 2.23606797749979

Comments

Popular posts from this blog

Coding standards?

programming language?

Computer programming languages?