-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroots-of-a-quadratic-equation
48 lines (43 loc) · 1.08 KB
/
roots-of-a-quadratic-equation
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
#include<iostream>
#include<math.h>
using namespace std:
int main()
{
float a,b,c,x1,x2,d,rp,ip;
cout<<"\n enter value of a: "; //"ax^2+bx+c=0"
cin>>a;
cout<<"\n enter value of b: ";
cin>>b;
cout<<"\n enter value of c: ";
cin>>c;
d = b*b -4*a*c; //discriminant
if (d>0)
{
x1 = (-b + sqrt(d))/(2*a); //first root
x2 = (-b + sqrt(d))/(2*a); //second root
cout<<"x1 = "<<x1;
cout<<"x2 = "<<x2;
}
else if (d==0)
{
cout<<"Both the Roots are equal";
x1 = -b/(2*a);
cout<<"x1 = x2 = "<<x1;
}
else
{
rp = -b/(2*a); //real part
ip = sqrt(-d)/(2*a); //imaginary part
cout<< "roots are complex and different.";
cout<<"x1 ="<<rp = "+"<<ip<<"i";
cout<<"x2 ="<<rp = "-"<<ip<<"i";
}
return 0;
}
/* OUTPUT OF PROGRAM
enter value of a: 1
enter value of b: 2
enter value of c: 1 //Putting value of 'a', 'b' and 'c' we get the value of 'd' as zero
Both the Roots are equal
x1 = x2 = -1
*/