-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadRoots.cpp
More file actions
63 lines (48 loc) · 1.53 KB
/
QuadRoots.cpp
File metadata and controls
63 lines (48 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Randall Hall
C++ program that will compute roots of a quadratic function, if they exist
*/
#include <cmath>
#include <iostream>
using namespace std;
double QuadRoot (double a, double b, double c)
{
double D;
D = (b*b) - (4*a*c);
return D;
}
int main () {
double a, b, c, D, D2, D3;
do {
cout << "Please enter a number between -100.0 and 100.0 for x^2: ";
cin >> a;
if (a < -100.0 || a > 100.0)
cout << "The number you have entered is not valid \n";
} while (a < -100.0 || a > 100.0);
do {
cout << "Please enter a number between -100.0 and 100.0 for x: ";
cin >> b;
if (b < -100.0 || b > 100.0)
cout << "The number you have entered is not valid \n";
} while (b < -100.0 || b > 100.0);
do {
cout << "Please enter a number between -100.0 and 100.0 for the constant: ";
cin >> c;
if (c < -100.0 || c > 100.0)
cout << "The number you have entered is not valid \n";
} while (c < -100.0 || c > 100.0);
cout << "\n\n";
D = QuadRoot(a, b, c);
D2 = (-b+sqrt(D))/(2*a);
D3 = (-b-sqrt(D))/(2*a);
if (D < 0) {
cout << "The determinant is less than 0 so the roots are imaginary/not real" << endl;
};
if (D == 0) {
cout << "The determinant is 0 so there will only be one root, the root is " << D2 << endl << endl;
};
if (D > 0) {
cout << "The determinant is greater than 0 so there will be two roots, the roots are " << D2 << " and " << D3 << endl << endl;
};
return 0;
}