Skip to content

Instantly share code, notes, and snippets.

@gilrg18
Created February 4, 2016 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilrg18/60c9549ab4342c154da5 to your computer and use it in GitHub Desktop.
Save gilrg18/60c9549ab4342c154da5 to your computer and use it in GitHub Desktop.
WSQ07 TC201 Babylonian Method
//Gilberto Rogel García A01630171
import javax.swing.*;
public class Babylonian {
public double x,
y=0;
public double bm(double a){
this.x=a;
while (this.x!= this.y){
this.y=this.x;
this.x=(a/this.x + this.x)/2;
//System.out.println(this.x);
}
return x;
}
public static void main(String[] args){
double p= Double.parseDouble(JOptionPane.showInputDialog("Give a number to get its square root"));
if (p==0){
System.out.println("The Square root is:"+" "+p);
}
else if (p<0){
System.out.println("Error");
}
else{
Babylonian banana= new Babylonian();
System.out.println("The Square root is:"+" "+banana.bm(p));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment