> For the complete documentation index, see [llms.txt](https://mayanktyagi3111.gitbook.io/interview-prep/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mayanktyagi3111.gitbook.io/interview-prep/binary-searching-and-sorting/find-square-root-of-number-upto-given-precision.md).

# Find square root of number upto given precision

Given a positive number n and precision p, find the square root of number upto p decimal places using binary search.\
\
**Examples:**

```
Input : number = 50, precision = 3
Output : 7.071

Input : number = 10, precision = 4
Output : 3.1622
```

**Approach :**\
**1)** As the square root of number lies in range 0 <= squareRoot <= number, therefore, initialize start and end as : start = 0, end = number.\
**2)** Compare the square of mid integer with the given number. If it is equal to the number, then we found our integral part, else look for the same in left or right side depending upon the scenario.\
**3)** Once we are done with finding the integral part, start computing the fractional part.\
**4)** Initialize the *increment* variable by 0.1 and iteratively compute the fractional part upto p places. For each iteration, *increment* changes to 1/10th of it’s previous value.\
**5)** Finally return the answer computed.<br>

```java
class Solution {
    public static double sqrt(int n, int precision) {
        double start = 0.0, end = n;
        while (end - start > Math.pow(10, -precision)) {
            double mid = start + (end - start) / 2;
            if (mid * mid <= n)
                start = mid;
            else
                end = mid;
        }
        return start;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mayanktyagi3111.gitbook.io/interview-prep/binary-searching-and-sorting/find-square-root-of-number-upto-given-precision.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
