2. คำนวณ

The Camtasia Studio video content presented here requires JavaScript to be enabled and the latest version of the Macromedia Flash Player. If you are you using a browser with JavaScript disabled please enable it now. Otherwise, please update your version of the free Flash Player by downloading here.

 
รหัสที่ 2-14  โปรแกรมหารากที่สองของจำนวนจริง ปรับปรุงให้ใช้แค่ตัวแปร x ตัวเดียว
import java.util.Scanner;  
// โปรแกรมหารากที่สองของจำนวนจริงด้วยวิธีของชาวบาบิโลน 
public class SquareRoot {
  public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("a = ");
    double a = kb.nextDouble();
    double x = 1;
    x = (x + a/x) / 2.0;
    x = (x + a/x) / 2.0;
    x = (x + a/x) / 2.0;
    x = (x + a/x) / 2.0;
    System.out.println("รากที่สองของ " + a + " = " + x);
  }
}
©2009 S.Prasitjutrakul