Skip to content

Commit ff5599e

Browse files
Merge pull request #39 from Krush2311/RootOfQuadratic
Root of quadratic
2 parents eb1948a + 70de58c commit ff5599e

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//Find the roots of the quadratic Equation
2+
import java.util.Scanner;
3+
4+
public class quadratic_roots {
5+
6+
public static void main(String[] args) {
7+
Scanner reader = new Scanner(System.in);
8+
double a = 0;
9+
double b = 0;
10+
double c = 0;
11+
double root1, root2;
12+
13+
System.out.println("Quadratic Equation : a(x)^2 + b(x) + c = 0");
14+
System.out.println("Enter value of a: ");
15+
a = reader.nextDouble();
16+
System.out.println("Enter value of b: ");
17+
b = reader.nextDouble();
18+
System.out.println("Enter value of c: ");
19+
c = reader.nextDouble();
20+
21+
// d stands for determinant
22+
double d = (b * b) - 4 * (a * c);
23+
24+
//if d = 0 roots are equal and real, id d>0 roots are real and distinct
25+
if (d >= 0) {
26+
root1 = (-b + Math.sqrt(d)) / (2 * a);
27+
root2 = (-b - Math.sqrt(d)) / (2 * a);
28+
29+
System.out.println("root1 = "+root1+ "\nroot2 = "+ root2);
30+
if (root1 == root2) {
31+
System.out.println("Both roots are equal and real!");
32+
}
33+
34+
else{
35+
System.out.println("Roots are real and distinct");
36+
}
37+
}
38+
//d<0, roots are imaginary
39+
else {
40+
double real = -b / (2 * a);
41+
double imaginary = Math.sqrt(-d) / (2 * a);
42+
System.out.println("root1 = "+real+ " + " +imaginary+"i");
43+
System.out.println("root2 = "+real+ " - " +imaginary+"i");
44+
System.out.println("Roots are imaginary!");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)