Skip to content

Instantly share code, notes, and snippets.

@matiasvillaverde
Created October 26, 2016 12:01
Show Gist options
  • Save matiasvillaverde/899d2b29d9a0ab8df6607db37d6194b9 to your computer and use it in GitHub Desktop.
Save matiasvillaverde/899d2b29d9a0ab8df6607db37d6194b9 to your computer and use it in GitHub Desktop.
/******************************************************************************
* Compilation: javac Quadratic.java
* Execution: java Quadatic b c
*
* 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
*
*
* % java Quadratic 1.0 1.0
* NaN
* NaN
*
*
******************************************************************************/
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment