Fractions represent parts of a whole. A fraction consists of a numerator (the top number) and a denominator (the bottom number). When the denominators of two fractions are different, they are called "unlike fractions." To add unlike fractions, you must first convert them into "like fractions" by finding a common denominator.
Steps to Add Unlike Fractions:
Find the Least Common Multiple (LCM): Determine the LCM of the two denominators. This LCM will be your common denominator.
Convert Fractions: For each fraction, multiply its numerator and denominator by the same number that transforms its original denominator into the LCM.
For the first fraction (num1 / den1): Multiply num1 and den1 by (LCM / den1).
For the second fraction (num2 / den2): Multiply num2 and den2 by (LCM / den2).
Add Numerators: Once you have like fractions, add their new numerators. The denominator remains the same (the LCM).
Simplify (Optional but Recommended): Reduce the resulting fraction to its simplest form by dividing both the numerator and the denominator by their Greatest Common Divisor (GCD).
Example:
Let's add 1/2 and 1/3.
Denominators: 2 and 3.
LCM: The LCM of 2 and 3 is 6.
Convert:
1/2 becomes (1*3) / (2*3) = 3/6
1/3 becomes (1*2) / (3*2) = 2/6
Add:3/6 + 2/6 = (3+2)/6 = 5/6.
Simplify:5/6 is already in its simplest form.
This calculator automates these steps to provide a quick and accurate sum of your unlike fractions. It's a useful tool for students learning fractions, educators, and anyone needing to perform fractional arithmetic efficiently.
// Helper function to calculate Greatest Common Divisor (GCD)
function gcd(a, b) {
var a = Math.abs(a);
var b = Math.abs(b);
while(b) {
var t = b;
b = a % b;
a = t;
}
return a;
}
// Helper function to calculate Least Common Multiple (LCM)
function lcm(a, b) {
if (a === 0 || b === 0) return 0;
return Math.abs(a * b) / gcd(a, b);
}
function calculateSum() {
var num1 = parseInt(document.getElementById("num1").value);
var den1 = parseInt(document.getElementById("den1").value);
var num2 = parseInt(document.getElementById("num2").value);
var den2 = parseInt(document.getElementById("den2").value);
var resultDiv = document.getElementById("result");
// Input validation
if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2)) {
resultDiv.innerHTML = "Please enter valid numbers.";
resultDiv.style.color = "#dc3545"; // Red for error
return;
}
if (den1 === 0 || den2 === 0) {
resultDiv.innerHTML = "Denominators cannot be zero.";
resultDiv.style.color = "#dc3545"; // Red for error
return;
}
// Ensure numerators are not negative if denominators are positive, or handle signs consistently
// For simplicity here, we assume standard positive fractions or handle signs within num/den multiplication
// If you need to support negative numerators/denominators, the logic becomes more complex.
var commonDenominator = lcm(den1, den2);
var convertedNum1 = num1 * (commonDenominator / den1);
var convertedNum2 = num2 * (commonDenominator / den2);
var sumNumerator = convertedNum1 + convertedNum2;
// Simplify the fraction
var commonDivisor = gcd(sumNumerator, commonDenominator);
var simplifiedNumerator = sumNumerator / commonDivisor;
var simplifiedDenominator = commonDenominator / commonDivisor;
// Handle potential negative results and display appropriately
var resultString = "";
if (simplifiedDenominator < 0) { // Ensure denominator is positive
simplifiedNumerator = -simplifiedNumerator;
simplifiedDenominator = -simplifiedDenominator;
}
if (simplifiedDenominator === 1) {
resultString = simplifiedNumerator.toString();
} else {
resultString = simplifiedNumerator + "/" + simplifiedDenominator;
}
resultDiv.innerHTML = "Sum: " + resultString;
resultDiv.style.color = "#28a745"; // Green for success
}