Created
February 4, 2016 18:41
-
-
Save gilrg18/60c9549ab4342c154da5 to your computer and use it in GitHub Desktop.
WSQ07 TC201 Babylonian Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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