Skip to content

Instantly share code, notes, and snippets.

@sdmg15
Created February 21, 2017 20:19
Show Gist options
  • Save sdmg15/171cce2e41b1c80aac11d8d29159c683 to your computer and use it in GitHub Desktop.
Save sdmg15/171cce2e41b1c80aac11d8d29159c683 to your computer and use it in GitHub Desktop.
Computing the square root of a given number using Newton method's.
import java.util.Scanner ;
public class sdz {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(squaroot(2,16,0.05));
}
static double squaroot(int start, double number , double precision){
double u0 = start;
double un;
do {
un = u0;
u0 = 0.5*(un + number/un);
}while(Math.abs(un-u0)> precision);
return u0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment