-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEquationSolver.cpp
More file actions
49 lines (41 loc) · 1.27 KB
/
EquationSolver.cpp
File metadata and controls
49 lines (41 loc) · 1.27 KB
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
#include <iostream>
#include <cmath>
using namespace std;
void InputVariables(double &a, double &b, double &c) {
cout << "Enter the variables in this format: a(x)^2 + bx + c = 0" << endl;
cout << "Enter a: ";
cin >> a;
cout << "Enter b: ";
cin >> b;
cout << "Enter c: ";
cin >> c;
}
double Discriminant(double a, double b, double c) {
return (b * b) - (4 * a * c);
}
void findRoots(double a, double b, double c) {
double d = Discriminant(a, b, c);
if (d > 0) {
double r1 = (-b + sqrt(d)) / (2 * a);
double r2 = (-b - sqrt(d)) / (2 * a);
cout << "Roots are real and distinct: " << r1 << " and " << r2 << endl;
}
else if (d == 0) {
double r = -b / (2 * a);
cout << "Roots are real and equal: " << r << endl;
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-d) / (2 * a);
cout << "Roots are complex:" << endl;
cout << realPart << " + " << imaginaryPart << "i" << endl;
cout << realPart << " - " << imaginaryPart << "i" << endl;
}
}
int main() {
cout << "======WELCOME TO THE ULTIMATE QUADRATIC EQUATIONS SOLVER======" << endl;
double a, b, c;
InputVariables(a, b, c);
findRoots(a, b, c);
return 0;
}