Calculators

Algebra Calculator

Algebra Calculator

Algebra Calculator

Solve equations, simplify expressions, and perform algebraic operations

Quadratic Formula

x = [-b ± √(b² - 4ac)] / 2a

For equations of the form: ax² + bx + c = 0

Result

Quick Examples (Click to try):

2x + 5 = 13
x² - 5x + 6 = 0
2x² + 3x - 2 = 0
x² - 4x + 4 = 0
x² + 2x + 5 = 0
3x + 2x - 5 + 3
2(3x + 4)
Coefficients: a = ${a}, b = ${b}, c = ${c}
Step 1: Calculate discriminant D = b² - 4ac
`; // Calculate discriminant const discriminant = b * b - 4 * a * c; solutionHTML += `
D = (${b})² - 4 × ${a} × ${c} = ${discriminant}
`; // Calculate solutions based on discriminant if (discriminant > 0) { // Two real solutions const sqrtD = Math.sqrt(discriminant); const x1 = (-b + sqrtD) / (2 * a); const x2 = (-b - sqrtD) / (2 * a); solutionHTML += `
Step 2: D > 0, so two real solutions exist
x₁ = [-b + √D] / 2a = [${-b} + ${sqrtD.toFixed(4)}] / ${2*a} = ${x1.toFixed(4)}
x₂ = [-b - √D] / 2a = [${-b} - ${sqrtD.toFixed(4)}] / ${2*a} = ${x2.toFixed(4)}
Solutions: x₁ = ${x1.toFixed(4)}, x₂ = ${x2.toFixed(4)}
`; } else if (discriminant === 0) { // One real solution const x = -b / (2 * a); solutionHTML += `
Step 2: D = 0, so one real solution exists (repeated root)
x = -b / 2a = ${-b} / ${2*a} = ${x.toFixed(4)}
Solution: x = ${x.toFixed(4)} (repeated root)
`; } else { // Complex solutions const realPart = -b / (2 * a); const imaginaryPart = Math.sqrt(-discriminant) / (2 * a); solutionHTML += `
Step 2: D < 0, so two complex solutions exist
x₁ = [-b + i√|D|] / 2a = [${-b} + i${Math.sqrt(-discriminant).toFixed(4)}] / ${2*a}
x₂ = [-b - i√|D|] / 2a = [${-b} - i${Math.sqrt(-discriminant).toFixed(4)}] / ${2*a}
Solutions: x₁ = ${realPart.toFixed(4)} + ${imaginaryPart.toFixed(4)}i, x₂ = ${realPart.toFixed(4)} - ${imaginaryPart.toFixed(4)}i
`; } showResult('Quadratic Equation Solution', solutionHTML); } catch (error) { showError('Error solving quadratic equation. Please check your input format.'); console.error(error); } } // Solve equation (linear) function solveEquation() { const input = document.getElementById('expression').value.trim(); if (!input) { showError('Please enter an equation to solve.'); return; } // Check if it's a quadratic equation if (input.includes('x^2') || input.includes('x²') || input.includes('x2')) { if (confirm('This appears to be a quadratic equation. Would you like to use the quadratic solver instead?')) { solveQuadratic(); return; } } // Check if it's an equation (contains =) if (!input.includes('=')) { showError('Please enter a valid equation with an equals sign (=).'); return; } const parts = input.split('='); if (parts.length !== 2) { showError('Invalid equation format. Use format: expression = expression'); return; } try { // Simple linear equation solver: ax + b = cx + d const left = parts[0].trim(); const right = parts[1].trim(); // Extract coefficients (simplified approach) let a = 0, b = 0, c = 0, d = 0; // Check for x terms and constants on both sides if (left.includes('x')) { const leftParts = left.split(/(?=[+-])/); for (let part of leftParts) { if (part.includes('x')) { const coeff = part.replace('x', '') || '1'; a += parseFloat(coeff); } else { b += parseFloat(part); } } } else { b = parseFloat(left); } if (right.includes('x')) { const rightParts = right.split(/(?=[+-])/); for (let part of rightParts) { if (part.includes('x')) { const coeff = part.replace('x', '') || '1'; c += parseFloat(coeff); } else { d += parseFloat(part); } } } else { d = parseFloat(right); } // Solve: ax + b = cx + d => (a-c)x = d-b => x = (d-b)/(a-c) const coefficient = a - c; const constant = d - b; let solutionHTML = ''; solutionHTML += `
Equation: ${input}
`; solutionHTML += `
Step 1: Move all terms to one side
`; solutionHTML += `
Step 2: Combine like terms
`; solutionHTML += `
Step 3: Isolate the variable
`; if (coefficient === 0) { if (constant === 0) { solutionHTML += `
The equation has infinitely many solutions (all real numbers).
`; } else { solutionHTML += `
The equation has no solution.
`; } } else { const solution = constant / coefficient; solutionHTML += `
Solution: x = ${solution}
`; } showResult('Equation Solution', solutionHTML); } catch (error) { showError('Error solving equation. Please check your input format.'); console.error(error); } } // Simplify expression function simplifyExpression() { const input = document.getElementById('expression').value.trim(); if (!input) { showError('Please enter an expression to simplify.'); return; } try { let simplified = input; // Remove spaces simplified = simplified.replace(/\s/g, ''); let resultHTML = `
Original: ${input}
`; // Combine like terms (basic implementation) if (simplified.includes('+') || simplified.includes('-')) { const terms = simplified.split(/(?=[+-])/); let xCoefficient = 0; let constant = 0; for (let term of terms) { if (term.includes('x')) { const coeff = term.replace('x', '') || (term.startsWith('-') ? '-1' : '1'); xCoefficient += parseFloat(coeff); } else { constant += parseFloat(term); } } // Build simplified expression let result = ''; if (xCoefficient !== 0) { result += (xCoefficient === 1 ? '' : (xCoefficient === -1 ? '-' : xCoefficient)) + 'x'; } if (constant !== 0) { if (result && constant > 0) result += ' + '; if (constant < 0) result += ' - '; result += Math.abs(constant); } if (!result) result = '0'; resultHTML += `
Step 1: Combine like terms
`; resultHTML += `
Step 2: Simplify coefficients
`; resultHTML += `
Simplified: ${result}
`; showResult('Simplified Expression', resultHTML); } else { showResult('Simplified Expression', `${resultHTML}
Expression is already simplified.
`); } } catch (error) { showError('Error simplifying expression. Please check your input format.'); console.error(error); } } // Evaluate expression function evaluateExpression() { const input = document.getElementById('expression').value.trim(); if (!input) { showError('Please enter an expression to evaluate.'); return; } try { const parsedExpr = parseExpression(input); // For safety, we'll only evaluate simple arithmetic expressions if (/[a-zA-Z]/.test(parsedExpr)) { showError('Evaluation with variables is not supported. Try using numbers only or use "Simplify" for expressions with variables.'); return; } const result = eval(parsedExpr); const resultHTML = `
Expression: ${input}
Step 1: Calculate inside parentheses
Step 2: Perform multiplication and division
Step 3: Perform addition and subtraction
Result: ${input} = ${result}
`; showResult('Evaluation Result', resultHTML); } catch (error) { showError('Error evaluating expression. Please check your input format.'); console.error(error); } } // Expand expression function expandExpression() { const input = document.getElementById('expression').value.trim(); if (!input) { showError('Please enter an expression to expand.'); return; } try { let expanded = input; let steps = []; steps.push(`
Original: ${input}
`); // Expand a(b+c) to ab + ac const expandPattern = /(\d*[a-zA-Z]?)\(([^()]+)\)/; let match; while ((match = expanded.match(expandPattern))) { const [full, factor, inner] = match; const terms = inner.split(/(?=[+-])/); let result = ''; for (let i = 0; i < terms.length; i++) { const term = terms[i]; if (term) { if (i > 0 && !term.startsWith('-') && !term.startsWith('+')) { result += '+'; } if (factor && factor !== '1') { if (factor === '-') { result += '-' + term; } else { result += factor + '*' + term; } } else { result += term; } } } steps.push(`
Step ${steps.length}: Expand ${full} → ${result}
`); expanded = expanded.replace(full, result); } steps.push(`
Expanded: ${expanded}
`); showResult('Expanded Expression', steps.join('')); } catch (error) { showError('Error expanding expression. Please check your input format.'); console.error(error); } } // Factor expression function factorExpression() { const input = document.getElementById('expression').value.trim(); if (!input) { showError('Please enter an expression to factor.'); return; } try { let factored = input; let resultHTML = `
Original: ${input}
`; // Simple factoring for quadratic expressions: ax² + bx + c if (input.includes('x^2') || input.includes('x²') || input.includes('x2')) { const coefficients = extractQuadraticCoefficients(input + '=0'); if (coefficients) { const { a, b, c } = coefficients; resultHTML += `
Coefficients: a=${a}, b=${b}, c=${c}
`; // Try to factor const factors = factorQuadratic(a, b, c); if (factors) { resultHTML += `
Factored: ${factors}
`; } else { resultHTML += `
Cannot be factored nicely. Use quadratic formula.
`; } } } else { factored = 'This calculator can only factor quadratic expressions.'; resultHTML += `
${factored}
`; } showResult('Factored Expression', resultHTML); } catch (error) { showError('Error factoring expression. Please check your input format.'); console.error(error); } } // Helper function to factor quadratic expressions function factorQuadratic(a, b, c) { // For simple cases where a=1 if (a === 1) { // Find two numbers that multiply to c and add to b for (let i = -Math.abs(c); i <= Math.abs(c); i++) { if (i !== 0 && c % i === 0) { const j = c / i; if (i + j === b) { return `(x + ${i})(x + ${j})`; } } } } // For difference of squares if (b === 0 && c < 0) { const sqrt = Math.sqrt(-c); if (Number.isInteger(sqrt)) { return `(x + ${sqrt}i)(x - ${sqrt}i)`; } } return null; } // Add keyboard support document.getElementById('expression').addEventListener('keypress', function(e) { if (e.key === 'Enter') { solveEquation(); } });
zaheerpashamd

Share
Published by
zaheerpashamd

Recent Posts

Audio Converter

🎧 aura convert · professional online audio converter aura convert client‑side MP3 · WAV ·…

6 months ago

Protect PDF

Protect PDF - Add Password | Secure & Free Protect PDF Add password protection to…

10 months ago

PNG to PDF Converter

PNG to PDF Converter PNG to PDF Converter Upload your PNG images and convert them…

10 months ago

PDF to PNG Converter

PDF to PNG Converter PDF to PNG Converter Upload your PDF file and convert it…

10 months ago

Advanced Image Resize Tool

Advanced Image Resize Tool - Free Online Image Resizer Home > Tools > Image Resize…

11 months ago

Geometry Calculator

Geometry Calculator Geometry Calculator Calculate area, perimeter, volume, and other properties of geometric shapes Select…

11 months ago