-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-quadratic.cpp
More file actions
53 lines (46 loc) · 1.37 KB
/
15-quadratic.cpp
File metadata and controls
53 lines (46 loc) · 1.37 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
#include <iostream>
#include <cmath> // For sqrt() function
using namespace std;
int main() {
double a, b, c;
double D, root1, root2;
int typeofroot;
// Input coefficients
cout << "Enter a coefficient: ";
cin >> a;
cout << "Enter b coefficient: ";
cin >> b;
cout << "Enter c coefficient: ";
cin >> c;
// Calculating the discriminant
D = b * b - 4 * a * c;
if (D > 0) {
typeofroot = 1; // Two real roots
} else if (D == 0) {
typeofroot = 2; // One real root
} else {
typeofroot = 3; // No real roots
}
// Use switch statement to handle the discriminant category
switch (typeofroot) {
case 1:
// For Two real roots
root1 = (-b + sqrt(D)) / (2 * a);
root2 = (-b - sqrt(D)) / (2 * a);
cout << "The quadratic equation has two real roots: " << root1 << " and " << root2 << endl;
break;
case 2:
//For One real root
root1 = -b / (2 * a);
cout << "The quadratic equation has one real root: " << root1 << endl;
break;
case 3:
//For Complex roots
cout << "The quadratic equation has no real roots." << endl;
break;
default:
cout << "Invalid value for Nature of root." << endl;
break;
}
return 0;
}