Python Challenges: Compute and return the square root of a given integer
Python Challenges - 1: Exercise-11 with Solution
Write a Python program to compute and return the square root of a given 'integer'.
Note: The returned value will be an ‘integer’.
Sample Solution:
Python Code:
def my_sqrt(x):
if x<2: return x
left=1
right=int(x/2)+1
while left<=right:
mid=int((left+right)/2)
if mid*mid==x:
return mid
if mid*mid>x:
right=mid-1
else:
left=mid+1
return right
print(my_sqrt(16))
Sample Output:
4
Flowchart:
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to find three numbers from an array such that the sum of three numbers equal to a given number.
Next: Write a Python program to find the single number in a list that doesn't occur twice.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/challenges/1/python-challenges-1-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics