Introduction
Equations are the bread and butter of math. Whether you’re calculating physics, handling algorithms, or just practicing, it’s super useful to know how to solve them with code. Let’s look at four cases:
- Linear equation (ax + b = 0)
- Quadratic equation (ax² + bx + c = 0)
- System of two linear equations (two unknowns)
- Cubic equation (ax³ + bx² + cx + d = 0)
For each, we’ll start with the math logic, then jump into C++ and Python code.
1. Linear Equation (ax + b = 0)
How to solve (math):
- Move b to the other side: ax = -b.
- If a = 0 and b = 0 → infinitely many solutions.
- If a = 0 and b ≠ 0 → no solution.
- Otherwise, x = -b/a.
C++:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b;
cin >> a >> b;
if (fabs(a) < 1e-9) {
if (fabs(b) < 1e-9) cout << "Infinite solutions\n";
else cout << "No solution\n";
} else {
cout << "x = " << -b/a << endl;
}
return 0;
}
Python:
a, b = map(float, input("Enter a, b: ").split())
if abs(a) < 1e-9:
if abs(b) < 1e-9:
print("Infinite solutions")
else:
print("No solution")
else:
print("x =", -b/a)
2. Quadratic Equation (ax² + bx + c = 0)
How to solve (math):
- Compute the discriminant: Δ = b² − 4ac.
- If Δ < 0: no real solution.
- If Δ = 0: one double root x = −b/(2a).
- If Δ > 0: two roots x₁, x₂ using the quadratic formula.
- If a = 0: reduce to linear.
C++:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
if (fabs(a) < 1e-9) {
cout << "x = " << -c/b << "\n";
} else {
double delta = b*b - 4*a*c;
if (delta < 0) cout << "No real roots\n";
else if (fabs(delta) < 1e-9) cout << "Double root x = " << -b/(2*a) << "\n";
else {
cout << "x1 = " << (-b + sqrt(delta))/(2*a) << "\n";
cout << "x2 = " << (-b - sqrt(delta))/(2*a) << "\n";
}
}
}
Python:
import math
a, b, c = map(float, input("Enter a, b, c: ").split())
if abs(a) < 1e-9:
print("x =", -c/b)
else:
delta = b*b - 4*a*c
if delta < 0:
print("No real roots")
elif abs(delta) < 1e-9:
print("Double root x =", -b/(2*a))
else:
x1 = (-b + math.sqrt(delta))/(2*a)
x2 = (-b - math.sqrt(delta))/(2*a)
print("x1 =", x1, "x2 =", x2)
3. System of Two Linear Equations
Form:
ax + by = c
dx + ey = f
How to solve (math):
- Use Cramer’s rule.
- Determinant: D = ae - bd.
- If D ≠ 0:x = (ce - bf) / D
y = (af - cd) / D
- x = (ce - bf) / D
- y = (af - cd) / D
- If D = 0:If both Dx = 0 and Dy = 0 → infinitely many solutions.
Otherwise → no solution.
- If both Dx = 0 and Dy = 0 → infinitely many solutions.
- Otherwise → no solution.
C++:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a,b,c,d,e,f;
cin >> a >> b >> c >> d >> e >> f;
double D = a*e - b*d;
double Dx = c*e - b*f;
double Dy = a*f - c*d;
if (fabs(D) < 1e-9) {
if (fabs(Dx) < 1e-9 && fabs(Dy) < 1e-9) cout << "Infinite solutions\n";
else cout << "No solution\n";
} else {
cout << "x = " << Dx/D << ", y = " << Dy/D << "\n";
}
}
Python:
a, b, c, d, e, f = map(float, input("Enter a,b,c,d,e,f: ").split())
D = a*e - b*d
Dx = c*e - b*f
Dy = a*f - c*d
if abs(D) < 1e-9:
if abs(Dx) < 1e-9 and abs(Dy) < 1e-9:
print("Infinite solutions")
else:
print("No solution")
else:
x = Dx / D
y = Dy / D
print("x =", x, "y =", y)
4. Cubic Equation (ax³ + bx² + cx + d = 0)
How to solve (math):
- General method: use Cardano’s formula.
- Substitute to remove the x² term: set x = t - b/(3a).
- Reduced form: t³ + pt + q = 0.
- Discriminant: Δ = (q/2)² + (p/3)³.If Δ > 0: 1 real root, 2 complex roots.
If Δ = 0: at least 2 equal real roots.
If Δ < 0: 3 distinct real roots.
- If Δ > 0: 1 real root, 2 complex roots.
- If Δ = 0: at least 2 equal real roots.
- If Δ < 0: 3 distinct real roots.
- Honestly, coding Cardano’s formula in C++ is a bit painful. In Python, we can just use NumPy.
C++ (simplified for one real root):
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a,b,c,d;
cin >> a >> b >> c >> d;
double f = ((3*c/a) - (b*b)/(a*a)) / 3;
double g = ((2*b*b*b)/(a*a*a) - (9*b*c)/(a*a) + (27*d/a)) / 27;
double h = g*g/4 + f*f*f/27;
if (h > 0) {
double R = -(g/2) + sqrt(h);
double S = cbrt(R);
double T = -(g/2) - sqrt(h);
double U = cbrt(T);
double x1 = (S+U) - (b/(3*a));
cout << "One real root x = " << x1 << "\n";
} else {
cout << "Multiple real roots (needs full Cardano)\n";
}
}
Python (NumPy):
import numpy as np
coeffs = list(map(float, input("Enter a, b, c, d: ").split()))
roots = np.roots(coeffs)
print("Roots:", roots)
Conclusion
We covered the math and code for solving linear, quadratic, 2×2 systems, and cubic equations. C++ builds the formulas by hand; Python keeps things concise (and NumPy finds cubic roots easily). Try implementing each solver yourself to build both algebra and programming skills.