Java program – Quadratic formula

by Nideesh C on April 28, 2011 · 0 comments

in Java Programming




public class Quadratic { 

    public static void main(String[] args) {
        double b = Double.parseDouble(args[0]);
        double c = Double.parseDouble(args[1]);

        double discriminant = b*b - 4.0*c;
        double sqroot =  Math.sqrt(discriminant);

        double root1 = (-b + sqroot) / 2.0;
        double root2 = (-b - sqroot) / 2.0;

        System.out.println(root1);
        System.out.println(root2);
    }
}


/*************************************************************************
 *
 *  Given b and c, solves for the roots of x*x + b*x + c.
 *  Assumes both roots are real valued.
 *
 *  % java Quadratic -3.0 2.0
 *  2.0
 *  1.0
 *
 *  % java Quadratic -1.0 -1.0
 *  1.618033988749895
 *  -0.6180339887498949
 *
 *  Remark:  1.6180339... is the golden ratio.
 *
 *  % java Quadratic 1.0 1.0
 *  NaN
 *  NaN
 *
 *
 *************************************************************************/



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C program to find and output all the roots of a quadratic equation, for non-zero coefficients.
  2. Sample – Program to find the roots of a quadratic equation.
  3. Java program – Integer multiplication and division
  4. Java program – String concatenation example
  5. C Program to find the roots of a quadratic equation

Leave a Comment

Previous post:

Next post: