-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathroots.cpp
35 lines (29 loc) · 923 Bytes
/
roots.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
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, D, real, imaginary;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
D = b*b - 4*a*c;
if (D > 0) {
x1 = (-b + sqrt(D)) / (2*a);
x2 = (-b - sqrt(D)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (D == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(D)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
real = -b/(2*a);
imaginary =sqrt(-D)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
return 0;
}