Fraction Calculator App

Fraction Calculator

Add (+) Subtract (-) Multiply (*) Divide (/)

Result:

Enter values and click 'Calculate'.

// Function to calculate Greatest Common Divisor (GCD) function gcd(a, b) { a = Math.abs(a); b = Math.abs(b); while (b) { var temp = b; b = a % b; a = temp; } return a; } // 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 to simplify a fraction function simplifyFraction(numerator, denominator) { if (denominator === 0) return [numerator, denominator]; // Undefined if (numerator === 0) return [0, 1]; // 0/x is 0 var common = gcd(numerator, denominator); var simplifiedN = numerator / common; var simplifiedD = denominator / common; // Ensure denominator is positive if (simplifiedD < 0) { simplifiedN = -simplifiedN; simplifiedD = -simplifiedD; } return [simplifiedN, simplifiedD]; } // Function to convert an improper fraction to a mixed number string function toMixedNumber(numerator, denominator) { if (denominator === 0) return "Undefined"; if (numerator === 0) return "0"; var simplified = simplifyFraction(numerator, denominator); var n = simplified[0]; var d = simplified[1]; var whole = Math.floor(Math.abs(n) / d); var remainderN = Math.abs(n) % d; var sign = n < 0 ? "-" : ""; if (whole === 0 && remainderN === 0) return "0"; if (whole === 0) return sign + remainderN + "/" + d; if (remainderN === 0) return sign + whole; return sign + whole + " " + remainderN + "/" + d; } // Main calculation function function calculateFraction() { var n1 = parseFloat(document.getElementById('numerator1').value); var d1 = parseFloat(document.getElementById('denominator1').value); var n2 = parseFloat(document.getElementById('numerator2').value); var d2 = parseFloat(document.getElementById('denominator2').value); var operation = document.getElementById('operation').value; var resultDisplay = document.getElementById('resultDisplay'); // Input validation if (isNaN(n1) || isNaN(d1) || isNaN(n2) || isNaN(d2)) { resultDisplay.innerHTML = "Please enter valid numbers for all fields."; return; } if (d1 === 0 || d2 === 0) { resultDisplay.innerHTML = "Denominator cannot be zero."; return; } var resultNumerator, resultDenominator; switch (operation) { case 'add': var commonDen = lcm(d1, d2); resultNumerator = n1 * (commonDen / d1) + n2 * (commonDen / d2); resultDenominator = commonDen; break; case 'subtract': var commonDen = lcm(d1, d2); resultNumerator = n1 * (commonDen / d1) – n2 * (commonDen / d2); resultDenominator = commonDen; break; case 'multiply': resultNumerator = n1 * n2; resultDenominator = d1 * d2; break; case 'divide': if (n2 === 0) { resultDisplay.innerHTML = "Cannot divide by zero."; return; } resultNumerator = n1 * d2; resultDenominator = d1 * n2; break; default: resultDisplay.innerHTML = "Invalid operation selected."; return; } // Handle potential division by zero from operations (e.g., 1/0 after simplification) if (resultDenominator === 0) { resultDisplay.innerHTML = "Result is undefined (division by zero)."; return; } var simplified = simplifyFraction(resultNumerator, resultDenominator); var simplifiedN = simplified[0]; var simplifiedD = simplified[1]; var mixedNumber = toMixedNumber(simplifiedN, simplifiedD); var output = "Original Result: " + resultNumerator + "/" + resultDenominator + ""; output += "Simplified Fraction: " + simplifiedN + "/" + simplifiedD + ""; output += "Mixed Number: " + mixedNumber; resultDisplay.innerHTML = output; }

Understanding Fractions and How to Calculate Them

Fractions are a fundamental concept in mathematics, representing a part of a whole. They consist of two main components: a numerator (the top number) and a denominator (the bottom number). The numerator tells us how many parts we have, while the denominator tells us how many equal parts make up the whole.

Types of Fractions

  • Proper Fractions: The numerator is smaller than the denominator (e.g., 1/2, 3/4).
  • Improper Fractions: The numerator is equal to or larger than the denominator (e.g., 5/4, 7/3).
  • Mixed Numbers: A combination of a whole number and a proper fraction (e.g., 1 1/2, 2 3/4).

Operations with Fractions

1. Adding Fractions

To add fractions, they must have a common denominator. If they don't, you need to find the Least Common Multiple (LCM) of the denominators and convert the fractions. Once they have the same denominator, you simply add the numerators and keep the denominator the same.

Formula: (a/b) + (c/d) = (ad + bc) / bd (or find LCM for common denominator)

Example: 1/2 + 1/4

  • LCM of 2 and 4 is 4.
  • Convert 1/2 to 2/4.
  • 2/4 + 1/4 = (2+1)/4 = 3/4.

Using the calculator: Enter 1 for Numerator 1, 2 for Denominator 1. Select 'Add'. Enter 1 for Numerator 2, 4 for Denominator 2. The result will be 3/4.

2. Subtracting Fractions

Similar to addition, fractions must have a common denominator for subtraction. Find the LCM if necessary, convert the fractions, and then subtract the numerators, keeping the denominator the same.

Formula: (a/b) – (c/d) = (ad – bc) / bd (or find LCM for common denominator)

Example: 3/4 – 1/2

  • LCM of 4 and 2 is 4.
  • Convert 1/2 to 2/4.
  • 3/4 – 2/4 = (3-2)/4 = 1/4.

Using the calculator: Enter 3 for Numerator 1, 4 for Denominator 1. Select 'Subtract'. Enter 1 for Numerator 2, 2 for Denominator 2. The result will be 1/4.

3. Multiplying Fractions

Multiplying fractions is straightforward: multiply the numerators together and multiply the denominators together. Simplify the resulting fraction if possible.

Formula: (a/b) * (c/d) = (a * c) / (b * d)

Example: 1/2 * 3/5

  • (1 * 3) / (2 * 5) = 3/10.

Using the calculator: Enter 1 for Numerator 1, 2 for Denominator 1. Select 'Multiply'. Enter 3 for Numerator 2, 5 for Denominator 2. The result will be 3/10.

4. Dividing Fractions

To divide fractions, you "flip" the second fraction (find its reciprocal) and then multiply it by the first fraction.

Formula: (a/b) / (c/d) = (a/b) * (d/c) = (a * d) / (b * c)

Example: 1/2 / 1/4

  • Flip 1/4 to 4/1.
  • 1/2 * 4/1 = (1 * 4) / (2 * 1) = 4/2.
  • Simplify 4/2 to 2/1 or 2.

Using the calculator: Enter 1 for Numerator 1, 2 for Denominator 1. Select 'Divide'. Enter 1 for Numerator 2, 4 for Denominator 2. The result will be 2/1 (simplified to 2).

Simplifying Fractions and Mixed Numbers

After performing operations, it's good practice to simplify the resulting fraction to its lowest terms. This involves dividing both the numerator and the denominator by their Greatest Common Divisor (GCD). For improper fractions, you can also convert them into mixed numbers, which often makes them easier to understand.

Our Fraction Calculator automatically simplifies the result and provides it as a mixed number when applicable, making complex fraction calculations quick and easy.

Leave a Comment