Skip to content

Instantly share code, notes, and snippets.

@BT-ICD
Created March 22, 2021 17:58
Show Gist options
  • Save BT-ICD/5e1a57ef331e5b43f7f24d4b3ed6d84c to your computer and use it in GitHub Desktop.
Save BT-ICD/5e1a57ef331e5b43f7f24d4b3ed6d84c to your computer and use it in GitHub Desktop.
To determine real roots of quadratic equation
/**
* Example to determine real roots of a Quadratic Equation
* The Standard Form of a Quadratic Equation looks like this: ax2 + bx + c =0
* d= b2 - 4ac
* When b2 − 4ac is positive, we get two Real solutions. There are two real roots.
* When it is zero we get just ONE real solution (both answers are the same). There is one real root.
* When it is negative we get a pair of Complex solutions. There are no real roots.
*
* Reference: https://www.mathsisfun.com/quadratic-equation-solver.html
* https://www.mathsisfun.com/algebra/quadratic-equation.html
* http://www.biology.arizona.edu/biomath/tutorials/quadratic/roots.html
* */
public class QuadraticEquationDemo {
public static void main(String[] args) {
double a, b, c, d;
double x1,y1;
a=1;
b=-3;
c=4;
d = Math.pow(b,2) - 4*a*c;
System.out.println("D is: " + d);
if(d>0){
x1 = (-b + Math.sqrt(d))/(2*a);
y1 = (-b - Math.sqrt(d))/(2*a);
System.out.println("Roots of equation:" + x1 + "," + y1);
}
else if (d==0){
x1= -b/(2*a);
System.out.println("Only one root of equation is :" + x1);
}
else{
System.out.println("No real roots");
}
}
}
@BT-ICD
Copy link
Author

BT-ICD commented Mar 22, 2021

###Sample Data
a=2;
b=5;
c=3;

Output:
D is: 1.0
Roots of equation:-1.0,-1.5

Sample Data:

a=-4;
b=12;
c=-9;

Output:
D is: 0.0
Only one root of equation is :1.5

Sample Data:

a=2;
b=-11;
c=5;

Output:
D is: 81.0
Roots of equation:5.0,0.5

Sample Data:

a=1;
b=-3;
c=4;

Output:
D is: -7.0
No real roots

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment