-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemE1.cpp
More file actions
82 lines (71 loc) · 1.89 KB
/
Copy pathProblemE1.cpp
File metadata and controls
82 lines (71 loc) · 1.89 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Jingi Min
CIS 22A 2023 Fall
Laboratory Assignment E
ProblemE1
get various values of a,b and c and find solution of equation
*/
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
double x; // solution
double determinant;
cout << "Input value of a, b and c of equation ax2 + bx + c: ";
cin >> a >> b >> c;
if (a == 0)
{
if ((b == 0) && (c == 0))
{
cout << "Any value of x is a solution." << endl;
}
else if ((b == 0) && !(c == 0))
{
cout << "No solution exists." << endl;
}
else if (!(b == 0))
{
x = -c / b;
cout << "Solution: " << x << endl;
}
}
else
{
determinant = pow(b, 2) - (4 * a * c);
if(determinant == 0){
x = (-b) / (2 * a);
cout << "Solution: " << x;
}
else if(determinant > 0){
x = (-b + sqrt(determinant)) / (2 * a);
cout << "Solution x1: " << x << endl;
x = (-b - sqrt(determinant)) / (2 * a);
cout << "Solution X2: " << x << endl;
}
else if(determinant < 0){
cout << "The solution have an imaginary component." << endl;
}
}
return 0;
}
/*
Execution results:
Input value of a, b and c of equation ax2 + bx + c: 0 0 0
Any value of x is a solution.
Input value of a, b and c of equation ax2 + bx + c: 0 0 4
No solution exists.
Input value of a, b and c of equation ax2 + bx + c: 0 8 -12
Solution: 1.5
Input value of a, b and c of equation ax2 + bx + c: 2 4 2
Solution: -1
Input value of a, b and c of equation ax2 + bx + c: 2 2 0
Solution x1: 0
Solution X2: -1
Input value of a, b and c of equation ax2 + bx + c: 100 100 -11
Solution x1: 0.1
Solution X2: -1.1
Input value of a, b and c of equation ax2 + bx + c: 1 1 1
The solution have an imaginary component.
*/