This program will solve quadratic equations. It accepts coefficients of a quadratic equation from the user i.e. a, b and c and displays the roots.
To compile the program name it quadratic_solver.cpp then type
g++ -o quadratic_solver quadratic_solver.cpp
You may need to use math.h like this: #include if you are using C++ compiler software on Windows. (I tried it without the <math.h>
and got an “undeclared identifier” error)
What is Quadratic Equation?
The Quadratic equation is the equation of the form as below:
ax2 + bx +c = 0
Where x represents unknown and a, b and c are coefficients, it’s roots is given by following the formula.
Here,
The term b2-4ac
is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.
- If discriminant is greater than 0, the roots are real and different.
- If discriminant is equal to 0, the roots are real and equal.
- If discriminant is less than 0, the roots are complex and different.
C++ Program to Solve 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 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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | /* program to find solution to quadratic equation in the standard form ax^+bx+c=0. Author David Tarsi. The logic portion of this program was developed from instruction. The coding is by the author. Any questions or comments welcome */ #include <iostream> #include <math.h> using namespace std; void one() { float a = 0.0; //here we declare the variables and use float because we float b = 0.0; //are dealing with square roots float c = 0.0; float x1 = 0.0; float x2 = 0.0; float x3 = 0.0; float x4 = 0.0; //this section gets user input and displays message cout << "Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:\n"; cout << "Enter value for a:\n"; cin >> a; cout << "Enter value for b:\n"; cin >> b; cout << "Enter value for c:\n"; cin >> c; //are all the coefficients 0? if so both roots are 0 if (a == 0 && b == 0 && c == 0) { x1 = 0; x2 = 0; cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //is c the only non-zero number? if so tell the user if (a == 0 && b == 0 && c != 0) { c = c; cout << "There are no roots" "\n" << "c = " << c << "\n"; } //is a zero? if so solve the resulting linear equations and notify user if (a == 0 && b != 0 && c != 0) { cout << "The values entered do not make a quadratic expression" "\n" << "x = " << -c / b << "\n"; } //if b is zero and c is zero tell user if (a == 0 && b != 0 && c == 0) { x1 = 0; x2 = 0; cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //if b and c are equal to zero then ax^=0 and since a cannot be zero without x being // zero also let user know if (a != 0 && b == 0 && c == 0) { x1 = 0; x2 = 0; cout << "The values entered result in ax^= 0; so both roots are 0" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //factor out x from ax^+bx=0 and either x = 0 or ax + b =0 //then solve the linear equation if (a != 0 && b != 0 && c == 0) { x1 = 0; x2 = -b / a; cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //now we get to use the square root function and let the user //know they have some imaginary numbers to deal with if (a < 0 && b == 0 && c < 0) { x1 = -b / (2 * a); x4 = (b * b) - (4 * a * c); x4 = -x4; x2 = sqrt(x4) / (2 * a); x3 = -sqrt(x4) / (2 * a); cout << "The roots are not real numbers:" "\n" << "x1 =" << x1 << " + " << x2 << " * i " << "\n" << "x2 =" << x1 << " + " << x3 << " * i " << "\n"; } if (a > 0 && b == 0 && c > 0) { x1 = -b / (2 * a); x4 = (b * b) - (4 * a * c); x4 = -x4; x2 = sqrt(x4) / (2 * a); x3 = -sqrt(x4) / (2 * a); cout << "The roots are not real numbers:" "\n" << "x1 =" << x1 << " + " << x2 << " * i " << "\n" << "x2 =" << x1 << " + " << x3 << " * i " << "\n"; } //now a and c are opposite signs so the answer will be real if (a > 0 && b == 0 && c < 0) { x1 = (-b + (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); x2 = (-b - (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } if (a < 0 && b == 0 && c > 0) { x1 = (-b + (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); x2 = (-b - (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //ok now if we end up not having to take the square root of a neg // do the math if (a != 0 && b != 0 && c != 0 && (4 * a * c) <= pow(b, 2)) { x1 = (-b + (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); x2 = (-b - (sqrt(pow(b, 2) - (4 * a * c)))) / (2 * a); cout << "The roots are:" "\n" << "x1 = " << x1 << " , " << "x2 = " << x2 << "\n"; } //here we have to deal with non x intercepts ie: sqrt(-1) // alter the formula slightly to give correct output and // let the user know if (a != 0 && b != 0 && c != 0 && (4 * a * c) > pow(b, 2)) { x1 = -b / (2 * a); x4 = (b * b) - (4 * a * c); x4 = -x4; x2 = sqrt(x4) / (2 * a); x3 = -sqrt(x4) / (2 * a); cout << "The roots are not real numbers" "\n" << "x1 =" << x1 << " + " << x2 << " * i " << "\n" << "x2 =" << x1 << " + " << x3 << " * i " << "\n"; } return; } //keep output from vanishing before we can read it. void two() { char c; cout << "Press c and then Enter to continue...." "\n"; cin >> c; for (;;) { if (c) { break; } } cout << "Done" "\n"; } int main() { one(); two(); return 0; } |
Output of C++ Program
Compile: $ g++ -o quadratic_solver quadratic_solver.cpp
Run:
$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
6
Enter value for b:
4
Enter value for c:
1
The roots are not real numbers
x1 =-0.333333 + 0.235702 * i
x2 =-0.333333 + -0.235702 * i
$ ./quadratic_solver
Enter the coefficients a , b , c for equation in the form ax^ + bx + c = 0:
Enter value for a:
9
Enter value for b:
24
Enter value for c:
2
The roots are:
x1 = -0.0861142 , x2 = -2.58055