fork(2) download
  1. class QuadraticSolver {
  2. public static double solveQuadratic(double a, double b, double c)
  3. {
  4. double D = b * b - 4 * a * c;
  5. if (D < 0)
  6. throw new RuntimeException(); // complex solution
  7.  
  8. return (-b + Math.sqrt(D)) / (2 * a);
  9. }
  10.  
  11. public static void main(String[] args)
  12. {
  13. System.out.println("x ^ 2 - 5x + 2 = 0");
  14. System.out.println("x = " + QuadraticSolver.solveQuadratic(1, -5, 2));
  15. }
  16. }
  17.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
x ^ 2 - 5x + 2 = 0
x = 4.561552812808831