Quadratic equation solver with double value - Java Language Basics

Java examples for Language Basics:Math

Description

Quadratic equation solver with double value

Demo Code

import java.util.Locale;
import java.util.Scanner;

public class Main {
    public static void main(String[] arg) {
        double a=1, b=1, c=1;
        double delta = b * b - 4.0 * a * c;
        double root1, root2;

        /*/*from  w w w  .j  ava  2  s  . c  o m*/
        // BEWARE: potentially Not a Number (NaN) for neg. discriminant!
        root1= (-b-Math.sqrt(delta))/(2.0*a);
        root2= (-b+Math.sqrt(delta))/(2.0*a);
        */
        if (delta >= 0.0d) {
            root1 = (-b - Math.sqrt(delta)) / (2.0 * a);
            root2 = (-b + Math.sqrt(delta)) / (2.0 * a);
            System.out.println("root1=" + root1 + " root2=" + root2);
        } else {
            System.out.println("Imaginary roots!");
        }
    }
}

Result


Related Tutorials