-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuadraticEquation.java
78 lines (54 loc) · 1.53 KB
/
QuadraticEquation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package practice;
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
System.out.println("Program to find real values for a Quadratic Equation.\n---");
Scanner sm = new Scanner(System.in);
System.out.println("Enter the integer values of a, b and c as for equation = ax2 + bx+ c:");
int a,b,c;
System.out.print("a: ");
a= sm.nextInt();
System.out.print("b: ");
b= sm.nextInt();
System.out.print("c: ");
c= sm.nextInt();
sm.close();
double d=discriminant(a,b,c);
if (d>0) {
System.out.println("D > 0\n2 Real Solutions.");
//if discriminant is real;
findRealValues(a,b,c,d);
}
else if (d==0) {
System.out.println("1 Real Solution.");
//if discriminant is real;
findRealValues(a,b,c,d);
}
else if (d<0) {
System.out.println("D < 0\n2 Imaginary Solutions.");
System.out.println("Cannot perform calculation as there are no Real Solutions.");
end();
}
else {
System.out.println("Unknown Error");
end();
}
}
private static double discriminant(int a, int b, int c) {
double d = ((b*b)-(4*a*c));
System.out.println("D = "+d);
return d;
}
private static void findRealValues(int a, int b, int c, double d) {
String ss = "";
double x1= (((-b)+(Math.sqrt(d)))/(2*a));
double x2= (((-b)-(Math.sqrt(d)))/(2*a));
ss = ss + x1 + ", " +x2;
System.out.println("Answer: "+ ss);
end();
}
private static void end() {
System.out.println("---\nThe Program has ended.");
System.exit(0);
}
}