-
Notifications
You must be signed in to change notification settings - Fork 0
/
RootsOfQuadraticEquation.cpp
65 lines (37 loc) · 1.44 KB
/
RootsOfQuadraticEquation.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int a, b, c, x1, x2, determinant, real, imaginary;
//taking coefficients of quadratic equation
cout<<"enter a, b and c respectively:";
cin>> a >> b >> c;
determinant= b*b - 4*a*c;
if (determinant>0){
x1=(-b+sqrt(determinant))/2*a;
x2=(-b-sqrt(determinant))/2*a;
cout<< "roots are real and different." <<endl;
cout<< "x1=" <<x1<<endl;
cout<< "x2=" <<x2<<endl;
}
else if (determinant==0) {
x1=(-b+sqrt(determinant))/2*a;
cout<< "roots are real and same." <<endl;
cout<< "x1=" <<x1<<endl;
cout<< "x2=" <<x1<<endl;
}
else {
real=b*b-4*a*c;
imaginary=sqrt(-determinant)/2*a;
cout<< "roots are imaginary and different." <<endl ;
cout<< "x1=" <<real<<"+"<<imaginary<<"i"<<endl;
cout<< "x2=" <<real<<"+"<<imaginary<<"i"<<endl;
}
return 0;
}