Enter your linear or quadratic equation in the formats: ax + b = c or ax^2 + bx + c = 0
Your solution will appear here.
Understanding the Algebra Equation Solver
This calculator is designed to solve fundamental algebraic equations, specifically linear and quadratic equations. Algebra is a branch of mathematics that uses symbols and letters to represent unknown quantities and relationships, forming the backbone of many scientific and technological disciplines.
Linear Equations (ax + b = c)
A linear equation in one variable, typically represented as ax + b = c, describes a relationship where the highest power of the variable (e.g., 'x') is one. Solving for 'x' means isolating it on one side of the equation. The process involves:
Subtracting 'b' from both sides: ax = c - b
Dividing both sides by 'a' (assuming 'a' is not zero): x = (c - b) / a
This solver identifies the coefficients 'a', 'b', and 'c' from your input and applies these steps to find the single solution for 'x'.
Quadratic Equations (ax^2 + bx + c = 0)
A quadratic equation is a polynomial equation where the highest power of the variable is two, generally in the form ax^2 + bx + c = 0. These equations can have zero, one, or two real solutions. The most common method for solving quadratic equations is the Quadratic Formula:
x = [-b ± sqrt(b^2 - 4ac)] / (2a)
The term b^2 - 4ac is known as the discriminant (Δ). Its value determines the nature of the roots:
If Δ > 0, there are two distinct real roots.
If Δ = 0, there is exactly one real root (a repeated root).
If Δ < 0, there are two complex conjugate roots (no real roots).
This calculator parses your input to extract 'a', 'b', and 'c', then calculates the discriminant and applies the quadratic formula to find the real solutions.
How to Use the Solver
To use the calculator:
Enter your equation in the provided text field. Ensure it's in one of the supported formats: ax + b = c for linear equations or ax^2 + bx + c = 0 for quadratic equations.
Click the "Solve Equation" button.
The solution(s) for 'x' will be displayed below the button. If there are no real solutions for a quadratic equation, it will indicate that.
This tool is useful for students learning algebra, mathematicians, engineers, and anyone who needs to quickly find the roots of simple algebraic equations.
function solveEquation() {
var equationInput = document.getElementById("equation").value.toLowerCase().replace(/\s+/g, ");
var resultDiv = document.getElementById("result");
resultDiv.style.backgroundColor = "#28a745"; // Default to success green
// Clear previous results
resultDiv.textContent = "Your solution will appear here.";
var solution = ";
// Regex to detect quadratic equation format (contains x^2)
var isQuadratic = equationInput.includes('x^2');
if (isQuadratic) {
// Attempt to parse quadratic equation: ax^2 + bx + c = 0
// This regex is simplified and assumes standard coefficient forms
// It tries to capture a, b, c even with missing terms or different signs
var quadraticRegex = /(-?\d*\.?\d*)x\^2([+-]\d*\.?\d*)x?([+-]\d*\.?\d*)?=0/;
var match = equationInput.match(quadraticRegex);
if (match) {
var aStr = match[1];
var bStr = match[2];
var cStr = match[3];
var a = parseFloat(aStr === " || aStr === '+' ? '1' : (aStr === '-' ? '-1' : aStr));
var b = parseFloat(bStr === undefined ? '0' : (bStr.startsWith('+') ? bStr.substring(1) : (bStr.startsWith('-') ? '-' + bStr.substring(1) : bStr)));
var c = parseFloat(cStr === undefined ? '0' : (cStr.startsWith('+') ? cStr.substring(1) : (cStr.startsWith('-') ? '-' + cStr.substring(1) : cStr)));
if (isNaN(a) || isNaN(b) || isNaN(c)) {
resultDiv.textContent = "Error: Could not parse coefficients.";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
return;
}
if (a === 0) {
// If a is 0, it's actually a linear equation bx + c = 0
if (b !== 0) {
var x = -c / b;
solution = "Linear case (a=0): x = " + formatNumber(x);
} else {
if (c === 0) {
solution = "Infinite solutions (0=0)";
} else {
solution = "No solution (" + c + "=0)";
resultDiv.style.backgroundColor = "#ffc107"; // Yellow for warning/no specific solution
}
}
} else {
var discriminant = b * b – 4 * a * c;
if (discriminant > 0) {
var x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
var x2 = (-b – Math.sqrt(discriminant)) / (2 * a);
solution = "x = " + formatNumber(x1) + " and x = " + formatNumber(x2);
} else if (discriminant === 0) {
var x = -b / (2 * a);
solution = "x = " + formatNumber(x) + " (repeated root)";
} else {
solution = "No real solutions (complex roots)";
resultDiv.style.backgroundColor = "#ffc107″; // Yellow for warning/no specific solution
}
}
} else {
// Try a simpler quadratic form like ax^2 + bx = 0 or ax^2 + c = 0
var simpleQuadraticRegex = /(-?\d*\.?\d*)x\^2([+-]\d*\.?\d*)x?([+-]\d*\.?\d*)?$/; // Assumes =0 at the end implicitly
var simpleMatch = equationInput.match(simpleQuadraticRegex);
if(simpleMatch) {
var aStr = simpleMatch[1];
var bStr = simpleMatch[2];
var cStr = simpleMatch[3];
var a = parseFloat(aStr === " || aStr === '+' ? '1' : (aStr === '-' ? '-1' : aStr));
var b = parseFloat(bStr === undefined ? '0' : (bStr.startsWith('+') ? bStr.substring(1) : (bStr.startsWith('-') ? '-' + bStr.substring(1) : bStr)));
var c = parseFloat(cStr === undefined ? '0' : (cStr.startsWith('+') ? cStr.substring(1) : (cStr.startsWith('-') ? '-' + cStr.substring(1) : cStr)));
if (isNaN(a) || isNaN(b) || isNaN(c)) {
resultDiv.textContent = "Error: Could not parse coefficients.";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
return;
}
if (a === 0) { // Should be handled by linear, but safety check
if (b !== 0) {
var x = -c / b;
solution = "Linear case (a=0): x = " + formatNumber(x);
} else {
if (c === 0) {
solution = "Infinite solutions (0=0)";
} else {
solution = "No solution (" + c + "=0)";
resultDiv.style.backgroundColor = "#ffc107";
}
}
} else {
var discriminant = b * b – 4 * a * c;
if (discriminant >= 0) {
var x1 = (-b + Math.sqrt(discriminant)) / (2 * a);
var x2 = (-b – Math.sqrt(discriminant)) / (2 * a);
if (Math.abs(x1 – x2) < 1e-9) { // Check if roots are very close
solution = "x = " + formatNumber(x1) + " (repeated root)";
} else {
solution = "x = " + formatNumber(x1) + " and x = " + formatNumber(x2);
}
} else {
solution = "No real solutions (complex roots)";
resultDiv.style.backgroundColor = "#ffc107";
}
}
} else {
resultDiv.textContent = "Error: Invalid quadratic equation format.";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
return;
}
}
} else {
// Attempt to parse linear equation: ax + b = c
var linearRegex = /(-?\d*\.?\d*)x([+-]\d*\.?\d*)?=([+-]?\d*\.?\d*)/;
var match = equationInput.match(linearRegex);
if (match) {
var aStr = match[1];
var bStr = match[2];
var cStr = match[3];
var a = parseFloat(aStr === '' || aStr === '+' ? '1' : (aStr === '-' ? '-1' : aStr));
// Handle b term being absent or just a sign
var b = parseFloat(bStr === undefined ? '0' : (bStr.startsWith('+') ? bStr.substring(1) : (bStr.startsWith('-') ? '-' + bStr.substring(1) : bStr)));
// Handle c term
var c = parseFloat(cStr === undefined ? '0' : (cStr.startsWith('+') ? cStr.substring(1) : (cStr.startsWith('-') ? '-' + cStr.substring(1) : cStr)));
if (isNaN(a) || isNaN(b) || isNaN(c)) {
resultDiv.textContent = "Error: Could not parse coefficients.";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
return;
}
if (a === 0) {
if (b === c) {
solution = "Infinite solutions";
resultDiv.style.backgroundColor = "#ffc107"; // Yellow for warning
} else {
solution = "No solution";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
}
} else {
var x = (c – b) / a;
solution = "x = " + formatNumber(x);
}
} else {
resultDiv.textContent = "Error: Invalid linear equation format.";
resultDiv.style.backgroundColor = "#dc3545"; // Red for error
return;
}
}
resultDiv.textContent = solution;
}
function formatNumber(num) {
// Formats numbers to a reasonable precision, avoiding excessive decimals
if (Math.abs(num) < 0.000001) return "0"; // Treat very small numbers as zero
if (num % 1 === 0) return num.toString(); // If it's an integer, return as is
return num.toFixed(6).replace(/\.?0+$/, ''); // Remove trailing zeros after decimal
}