Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save devteampentagon/e68089b1e19f4ec3257b21c5368f1120 to your computer and use it in GitHub Desktop.
Save devteampentagon/e68089b1e19f4ec3257b21c5368f1120 to your computer and use it in GitHub Desktop.
Computing the square root of a given number using Newton method
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