-
Notifications
You must be signed in to change notification settings - Fork 10
/
12_quadratic_equation.cpp
77 lines (61 loc) · 2.23 KB
/
12_quadratic_equation.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
66
67
68
69
70
71
72
73
74
75
76
77
/*
Given coefficients of a quadratic equation , you need to print the nature of the roots (Real and Distinct , Real and Equal or Imaginary) and the roots.
If Real and Distinct , print the roots in increasing order.
If Real and Equal , print the same repeating root twice
If Imaginary , no need to print the roots.
Note : Print only the integer part of the roots.
Input Format: First line contains three integer coefficients a,b,c for the equation ax^2 + bx + c = 0.
Constraints: -100 <= a, b, c <= 100
Output Format: Output contains one/two lines.
First line contains nature of the roots .
The next line contains roots(in non-decreasing order) separated by a space if they exist.
If roots are imaginary do not print the roots. Output the integer values for the roots.
Sample Input: 1 -11 28
Sample Output: Real and Distinct
4 7
Explanation: The input corresponds to equation ax^2 + bx + c = 0. Roots = (-b + sqrt(b^2 - 4ac))/2a , (-b - sqrt(b^2 - 4ac))/2a
*/
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int a,b,c;
cin>>a>>b>>c;
int d = b*b-(4*a*c);
int d_root = sqrt(d);
if(d>0){
cout<<"Real and Distinct";
}else if(d==0){
cout<<"Real and Equal";
}else{
cout<<"Imaginary";
}
cout << endl;
if(d>=0){
int root_1 = (-b - d_root)/(2*a);
int root_2 = (-b + d_root)/(2*a);
cout<<root_1<<" "<<root_2;
}
return 0;
}
/*
Formulas for Solving Quadratic Equations:
1. The roots of the quadratic equation: x = (-b ± √D)/2a, where D = b2 – 4ac
2. Nature of roots:
D > 0, roots are real and distinct (unequal)
D = 0, roots are real and equal (coincident)
D < 0, roots are imaginary and unequal
Algo:
1 Calculate the D viz, sqrt(b * b - 4 * a * c).
2 If D < 0,
- print Imaginary
3 if D == 0,
- print Real and Equal
- Calculate roots using formula ( - b + D) / (2 * a) and (- b - D)/ (2 * a)
- Print the roots
4 If D > 0,
- print Real and Distinct
- Calculate roots using formula ( - b + D) / (2 * a) and (- b - D)/ (2 * a)
- print the roots (in non-decreasing order)
5 End
*/