-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnagasai6.cpp
116 lines (105 loc) · 3.44 KB
/
nagasai6.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <cmath>
using namespace std;
class EquationSolver {
public:
void solve() {
int choice;
while (true) {
cout << "\nEquation Solver Menu:\n";
cout << "1. 2D Linear (Two variables)\n";
cout << "2. 3D Linear (Three variables)\n";
cout << "3. 4D Linear (Four variables)\n";
cout << "4. Quadratic (ax^2 + bx + c = 0)\n";
cout << "0. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
solve2D();
break;
case 2:
solve3D();
break;
case 3:
solve4D();
break;
case 4:
solveQuadratic();
break;
case 0:
cout << "Exiting...\n";
return;
default:
cout << "Invalid choice! Please try again.\n";
break;
}
}
}
double determinant(double mat[4][4], int n) {
double det = 0;
double temp[4][4];
if (n == 2) {
return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0];
}
else {
for (int p = 0; p < n; p++) {
int h = 0, k = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == p)
continue;
temp[h][k] = mat[i][j];
k++;
if (k == n - 1) {
h++;
k = 0;
}
}
}
det = det + mat[0][p] * pow(-1, p) * determinant(temp, n - 1);
}
}
return det;
}
void solve2D() {}
void solve3D() {}
void solve4D() {}
void solveQuadratic() {
double a, b, c;
cout << "Enter coefficients for quadratic equation (ax^2 + bx + c = 0): ";
cin >> a >> b >> c;
if (a == 0) {
if (b == 0) {
if (c == 0) {
cout << "Infinite solutions (all real numbers)." << endl;
} else {
cout << "No solution." << endl;
}
} else {
double root = -c / b;
cout << "Single real root: x = " << root << endl;
}
} else {
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
cout << "Two real roots: x1 = " << root1 << ", x2 = " << root2 << endl;
} else if (discriminant == 0) {
double root = -b / (2 * a);
cout << "Single real root: x = " << root << endl;
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
cout << "Complex roots: x1 = " << realPart << " + " << imaginaryPart << "i, "
<< "x2 = " << realPart << " - " << imaginaryPart << "i" << endl;
}
}
}
};
int main() {
EquationSolver solver;
solver.solve();
return 0;
}