Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. package newton_sqrt;
  2. import java.util.Scanner;
  3.  
  4.  
  5. public class sqrt {
  6.    
  7. public static void main(String [] args) {
  8.    
  9.     Scanner obj = new Scanner(System.in);
  10.     System.out.println("Enter in N for Newton:");
  11.     double N = obj.nextDouble();
  12.     System.out.println("Newton("+N+".O)="+getSqrt(N));
  13.     System.out.println("Math.sqrt="+Math.sqrt(N));}
  14.    
  15.    
  16.     static double getSqrt(double N) {
  17.         double x=N, absolute_value_of_x=0;
  18.         double new_guess=0;
  19.         double last_guess=N/2;
  20.         double error = last_guess;
  21.        
  22.         while(error>0.000001) {
  23.         new_guess = (N/last_guess + last_guess)/2;
  24.         error = Math.abs(last_guess-new_guess);
  25.         last_guess = new_guess;
  26.         }
  27.     return new_guess;
  28.        
  29.     }
  30.    
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement