Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package newton_sqrt;
- import java.util.Scanner;
- public class sqrt {
- public static void main(String [] args) {
- Scanner obj = new Scanner(System.in);
- System.out.println("Enter in N for Newton:");
- double N = obj.nextDouble();
- System.out.println("Newton("+N+".O)="+getSqrt(N));
- System.out.println("Math.sqrt="+Math.sqrt(N));}
- static double getSqrt(double N) {
- double x=N, absolute_value_of_x=0;
- double new_guess=0;
- double last_guess=N/2;
- double error = last_guess;
- while(error>0.000001) {
- new_guess = (N/last_guess + last_guess)/2;
- error = Math.abs(last_guess-new_guess);
- last_guess = new_guess;
- }
- return new_guess;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement