Skip to content

Commit 6c6f1af

Browse files
authored
Merge pull request #478 from ushmita4/master
#433 Finding Square Root of a number using Newton Rhapson Method.
2 parents 4c74e72 + 2ae2652 commit 6c6f1af

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Numbers/SquareRoot.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.io.*;
2+
public class SquareRoot
3+
{
4+
/* Time Comeplexity:-O(lg(n))
5+
Space Complexity:- O(1)**/
6+
public static void main(String args[])throws IOException
7+
{
8+
InputStreamReader read=new InputStreamReader(System.in);
9+
BufferedReader in=new BufferedReader(read);
10+
System.out.println("Enter your number");
11+
int n=Integer.parseInt(in.readLine());
12+
int x=n;
13+
if(x<=1)
14+
System.out.println(x);
15+
int sqrt=x/2;
16+
int quotient=x/sqrt;
17+
while(sqrt>quotient)
18+
{
19+
sqrt=(sqrt+quotient)>>1;//bitwise operator used to make the process faster
20+
quotient=x/sqrt;
21+
}
22+
System.out.println("The square root of the number "+n+" is = "+sqrt);
23+
}
24+
}

0 commit comments

Comments
 (0)