Skip to content

Commit 620037c

Browse files
authored
Solution Quadratic
1 parent fb03526 commit 620037c

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)