-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrt_bs.java
More file actions
37 lines (37 loc) · 1000 Bytes
/
Sqrt_bs.java
File metadata and controls
37 lines (37 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.*;
public class Sqrt_bs {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to find its square root:");
int n = sc.nextInt();
System.out.println("Enter the precision (number of decimal places): ");
int p = sc.nextInt();
System.out.printf("Square root of " + n + " is: %.3f", sqrt(n, p));
}
static double sqrt(int n, int p) {
int start = 0;
int end = n;
double root = 0.0;
while (start <= end) {
int mid = start + (end - start) / 2;
if (mid * mid == n) {
return mid;
}
if (mid * mid < n) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
double incr = 0.1;
for (int i = 0; i < p; i++) {
while (root * root <= n) {
root = root + incr;
}
root = root - incr;
incr = incr / 10;
}
return root;
}
}