-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCPP-07-Quadratic-Equation.txt
More file actions
68 lines (55 loc) · 1.35 KB
/
Copy pathCPP-07-Quadratic-Equation.txt
File metadata and controls
68 lines (55 loc) · 1.35 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
1. Define headers
2. Define class quadratic with private variables and public functions
3. Define input() function to get user input
4. Define compute() function to calculate the roots
-> calculate discriminant value
-> if d==0, then roots are equal (r1=r2)
-> if d>0, then roots are real and distinct
-> if d<0, then roots are complex
5. Define output() function to display result
6. Define main() function to invoke all class functions
*/
#include<iostream>
#include<math.h>
using namespace std;
class quadratic {
private:
double a,b,c,d,r1,r2;
public:
void input();
void compute();
void output();
};
void quadratic::input(){
cout<<"Enter the co-efficients of a,b,c";
cin>>a>>b>>c;
}
void quadratic::compute(){
d= b*b - 4*a*c;
if(d==0)
{
cout<<"Roots are equal"<<endl;
r1= -b / (2*a);
r2=r1;
}
else if(d>0){
cout<<"Roots are real and distinct"<<endl;
r1=(-b + sqrt(d)) / (2*a);
r2=(-b - sqrt(d)) / (2*a);
}
else {
cout<<"Roots are imaginary"<<endl;
exit(0);
}
}
void quadratic::output(){
cout<<"First root = "<<r1<<endl;
cout<<"Second root = "<<r2<<endl;
}
int main(){
quadratic q;
q.input();
q.compute();
q.output();
}