-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadratic_roots.cpp
47 lines (36 loc) · 1.09 KB
/
quadratic_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
36
37
38
39
40
41
42
43
44
45
46
47
// Author: Steven Peters
// July 21, 2019
// This program calculates the roots of a quadratic function
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
double a, b, c; // Quadratic coefficients
double root1, root2;
cout << "Enter the coefficients of the quadratic equation ax*x + bx + c = 0" << endl;
cin >> a >> b >> c;
double discriminant = (b*b) - 4*a*c;
if(a!=0) {
if(discriminant >= 0) {
if(discriminant == 0) {
root1 = -b/(2*a);
cout << "The equation has 1 real root:\nx = " << root1 << endl;
}
if(discriminant > 0) {
root1 = -b/(2*a) - sqrt(discriminant)/(2*a);
root2 = -b/(2*a) + sqrt(discriminant)/(2*a);
cout << "The equation has 2 real roots:\nx = " << root1 << "\nx = " << root2 << endl;
}
}
if(discriminant <= 0) {
double real = -b/(2*a);
double imaginary = sqrt( fabs(discriminant) ) / (2*a);
cout << "The equation has complex roots: \nx = " << real << " + " << imaginary << "i"
<< "\nx = " << real << " - " << imaginary << "i" << endl;
}
}
else
cout << "The coefficient 'a' cannot equal zero" << endl;
return 0;
}