A quadratic equation solver - Java Language Basics

Java examples for Language Basics:Operator

Description

A quadratic equation solver

Demo Code

public class Main {
  public static void main(String[] arg) {
    double a, b, c;
    a = Math.sqrt(3.0);/*  w ww  .ja v a 2 s .c o m*/
    b = 2.0;
    c = -3.0;
    double delta = b * b - 4.0 * a * c;
    double root1, root2;
    root1 = (-b - Math.sqrt(delta)) / (2.0 * a);
    root2 = (-b + Math.sqrt(delta)) / (2.0 * a);
    System.out.println(root1);
    System.out.println(root2);
    System.out.println("Let us check the roots:");
    System.out.println(a * root1 * root1 + b * root1 + c);
    System.out.println(a * root2 * root2 + b * root2 + c);
  }
}

Result


Related Tutorials