Find square root of a number without using sqrt() function
Find square root of a number without using sqrt() function
Given a number N, the task is to find the square root of N without using sqrt() function.
Examples:
Input: N = 25
Output: 5
Input: N = 25
Output: 5
Input: N = 2.5
Output: 1.58114
Approaches:
Output: 1.58114
Approaches:
- Start iterating from i = 1. If i * i = n, then print i as n is a perfect square whose square root is i.
- Else find the smallest i for which i * i is strictly greater than n.
- Now we know square root of n lies in the interval i – 1 and i and we can use Binary Search algorithm to find the square root.
- Find mid of i – 1 and i and compare mid * mid with n, with precision upto 5 decimal places.
- If mid * mid = n then return mid.
- If mid * mid < n then recur for the second half.
- If mid * mid > n then recur for the first half.
Comments
Post a Comment
good try..wait for the answer