Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to find square root of a integer number without using square root function
- import time
- def squareRoot(number,sqroot):
- # setting answer to be accurate upto 6 decimal places
- # change this to get more accuracy
- error = 0.000001
- # calculating num
- num = number/sqroot
- # till the difference is greater than error keep taking average
- while abs(num-sqroot) > error:
- sqroot = (sqroot+num)/2
- num = number/sqroot
- print(sqroot)
- print(num)
- # if num and sqroot are equal then we directly print the answer
- if num == sqroot:
- break
- print("The Square Root of " + str(number) + " is " + str(sqroot))
- return
- # main function
- cont = True
- while(cont):
- number = float(input("Enter the number whose square root is to be found : "))
- #if the number is less than 0 then ask user to enter another number
- while number < 0:
- print("Enter a number greater than 0")
- number = int(input("Enter the number whose square root is to be found : "))
- # if the number is 0 or 1 then the answer is also is also 0 or 1
- if number == 0 or number == 1:
- print("The Square Root of " + str(number) + " is " + str(number))
- # else take initial guess from the user
- else:
- sqroot = int(input("Enter the initial guess : "))
- sqroot = sqroot%number
- squareRoot(number,sqroot)
- n = int(input("Do you want to enter another number (1/0): "))
- if n == 0:
- cont = False
Add Comment
Please, Sign In to add comment