Algorithm
1. Input: Take a number for which you want to find the square root.
2. Initialize: Set an initial guess for the square root. This could be the number itself or any reasonable approximation.
3. Update: Use the formula guess = 0.5 * (guess + number/guess) iteratively to improve the guess. Repeat this step until the guess is close enough to the actual square root.
4. Output: The final value of the guess is the square root of the input number.
Code Examples
#1 Code Example-Python program For positive numbers
Code -
Python Programming
# Python Program to calculate the square root
# Note: change this value for a different result
num = 8
# To take the input from the user
#num = float(input('Enter a number: '))
num_sqrt = num ** 0.5
print('The square root of %0.3f is %0.3f'%(num ,num_sqrt))
Copy The Code &
Try With Live Editor
Output
#2 Code Example-Python Program For real or complex numbers
Code -
Python Programming
# Find square root of real or complex numbers
# Importing the complex math module
import cmath
num = 1+2j
# To take input from the user
#num = eval(input('Enter a number: '))
num_sqrt = cmath.sqrt(num)
print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Find the Square Root-DevsEnv