-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathSquareRoot.java
More file actions
24 lines (24 loc) · 810 Bytes
/
SquareRoot.java
File metadata and controls
24 lines (24 loc) · 810 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
import java.io.*;
public class SquareRoot
{
/* Time Comeplexity:-O(lg(n))
Space Complexity:- O(1)**/
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
System.out.println("Enter your number");
int n=Integer.parseInt(in.readLine());
int x=n;
if(x<=1)
System.out.println(x);
int sqrt=x/2;
int quotient=x/sqrt;
while(sqrt>quotient)
{
sqrt=(sqrt+quotient)>>1;//bitwise operator used to make the process faster
quotient=x/sqrt;
}
System.out.println("The square root of the number "+n+" is = "+sqrt);
}
}