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-9  โปรแกรมหารากที่สองของสมการกำลังสอง ax2 + bx + c = 0
import java.util.Scanner;  
// โปรแกรมหารากของสมการ ax2 + bx + c = 0
public class QuadraticRoot{
 public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    System.out.print("a = ");
    double a = kb.nextDouble();
    System.out.print("b = ");
    double b = kb.nextDouble();
    System.out.print("c = ");
    double c = kb.nextDouble();
    double r1 = ((-b) + Math.sqrt((b*b) - (4*a*c))) / (2*a);
    double r2 = ((-b) - Math.sqrt((b*b) - (4*a*c))) / (2*a);
    System.out.print("รากของสมการคือ ");
    System.out.println(r1 + " และ " + r2);
  }
}
©2009 S.Prasitjutrakul