Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1.  
  2. import java.util.Scanner;
  3.  
  4. public class Sqrt {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner sc = new Scanner(System.in);
  9.         double num = sc.nextInt();
  10.         sc.close();
  11.  
  12.         boolean flag = true;
  13.         double left = 0;
  14.         double right = 1e10;
  15.         double mid = 0;
  16.         double root = 0;
  17.  
  18.         while (flag) {
  19.             mid = (left + right) / 2;
  20.             if (mid * mid - num < 0.00001) {
  21.                 flag = false;
  22.                 root = mid;
  23.             } else {
  24.                 if (mid * mid > num)
  25.                     right = mid;
  26.                 else
  27.                     left = mid;
  28.             }
  29.         }
  30.         System.out.println(root);
  31.  
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement