-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathRootsOfQuadraticEq.cpp
More file actions
26 lines (21 loc) · 895 Bytes
/
RootsOfQuadraticEq.cpp
File metadata and controls
26 lines (21 loc) · 895 Bytes
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
#include <iostream>
#include <cmath>
int main() {
double a, b, c;
std::cout << "Enter the coefficients of the quadratic equation (a, b, c): ";
std::cin >> a >> b >> c;
double discriminant = b * b - 4 * a * c;
if (discriminant > 0) {
double root1 = (-b + sqrt(discriminant)) / (2 * a);
double root2 = (-b - sqrt(discriminant)) / (2 * a);
std::cout << "Two real roots: " << root1 << " and " << root2 << std::endl;
} else if (discriminant == 0) {
double root = -b / (2 * a);
std::cout << "One real root: " << root << std::endl;
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
std::cout << "Complex roots: " << realPart << " + " << imaginaryPart << "i and " << realPart << " - " << imaginaryPart << "i" << std::endl;
}
return 0;
}