Fraction Calculator Whole Numbers

Fraction Calculator (Whole Numbers) :root { –primary-blue: #004a99; –success-green: #28a745; –light-background: #f8f9fa; –dark-text: #333; –border-color: #ccc; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(–light-background); color: var(–dark-text); line-height: 1.6; margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center; } .loan-calc-container { background-color: #fff; border: 1px solid var(–border-color); border-radius: 8px; padding: 30px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05); width: 100%; max-width: 600px; margin-bottom: 30px; } h1, h2 { color: var(–primary-blue); text-align: center; margin-bottom: 20px; } .input-group { margin-bottom: 20px; display: flex; flex-direction: column; gap: 8px; } .input-group label { font-weight: bold; color: var(–primary-blue); } .input-group input[type="number"] { padding: 10px; border: 1px solid var(–border-color); border-radius: 4px; width: 100%; box-sizing: border-box; font-size: 1rem; } .fraction-inputs { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 15px; margin-bottom: 15px; } .fraction-part { border: 1px solid var(–border-color); padding: 10px; border-radius: 4px; background-color: var(–light-background); text-align: center; } .fraction-part label { display: block; margin-bottom: 5px; font-size: 0.9em; color: var(–primary-blue); } .fraction-part input[type="number"] { width: calc(100% – 20px); /* Adjust for padding */ margin: 0 auto; border: none; background-color: transparent; text-align: center; font-size: 1.1rem; padding: 5px; } .fraction-part input[type="number"]:focus { outline: none; border-bottom: 1px solid var(–primary-blue); } .operation-select { margin-top: 10px; margin-bottom: 20px; display: flex; flex-direction: column; gap: 8px; } .operation-select label { font-weight: bold; color: var(–primary-blue); } .operation-select select { padding: 10px; border: 1px solid var(–border-color); border-radius: 4px; font-size: 1rem; background-color: white; cursor: pointer; } button { background-color: var(–primary-blue); color: white; border: none; padding: 12px 25px; border-radius: 4px; font-size: 1.1rem; cursor: pointer; transition: background-color 0.3s ease; width: 100%; margin-top: 15px; } button:hover { background-color: #003b7a; } #result { margin-top: 30px; padding: 20px; background-color: var(–success-green); color: white; text-align: center; border-radius: 4px; font-size: 1.5rem; font-weight: bold; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .article-section { background-color: #fff; border: 1px solid var(–border-color); border-radius: 8px; padding: 30px; width: 100%; max-width: 800px; margin-top: 30px; } .article-section h2 { color: var(–primary-blue); text-align: left; } .article-section p { margin-bottom: 15px; } .article-section code { background-color: #e9ecef; padding: 2px 5px; border-radius: 3px; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; } /* Responsive adjustments */ @media (max-width: 768px) { .loan-calc-container, .article-section { padding: 20px; } .fraction-inputs { grid-template-columns: 1fr 1fr; } button { font-size: 1rem; padding: 10px 20px; } #result { font-size: 1.3rem; } } @media (max-width: 480px) { .fraction-inputs { grid-template-columns: 1fr; } h1 { font-size: 1.8rem; } }

Fraction Calculator (Whole Numbers)

+ – * /
Result will appear here

Understanding Fraction Arithmetic with Whole Numbers

Fractions represent parts of a whole. They consist of a numerator (the top number) and a denominator (the bottom number), separated by a fraction bar. The denominator indicates how many equal parts the whole is divided into, and the numerator indicates how many of those parts are being considered. This calculator focuses on performing basic arithmetic operations (addition, subtraction, multiplication, and division) between two fractions, where the inputs are treated as whole numbers for the numerators and denominators.

Operations Explained:

  • Addition: To add two fractions, a/b + c/d, they must have a common denominator. The formula is (ad + bc) / bd. For example, 1/2 + 1/3 = (1*3 + 1*2) / (2*3) = (3 + 2) / 6 = 5/6.
  • Subtraction: Similar to addition, to subtract a/b - c/d, you find a common denominator. The formula is (ad - bc) / bd. For example, 1/2 - 1/3 = (1*3 - 1*2) / (2*3) = (3 - 2) / 6 = 1/6.
  • Multiplication: Multiplying fractions, a/b * c/d, is straightforward. You multiply the numerators together and the denominators together: (a * c) / (b * d). For example, 1/2 * 1/3 = (1 * 1) / (2 * 3) = 1/6.
  • Division: Dividing by a fraction is equivalent to multiplying by its reciprocal. For a/b / c/d, you multiply the first fraction by the reciprocal of the second: a/b * d/c = (a * d) / (b * c). For example, 1/2 / 1/3 = 1/2 * 3/1 = (1 * 3) / (2 * 1) = 3/2.

Simplifying Fractions (Greatest Common Divisor – GCD):

After performing an operation, the resulting fraction can often be simplified. This is done by finding the Greatest Common Divisor (GCD) of the numerator and the denominator, and then dividing both by it. For example, if the result is 4/6, the GCD of 4 and 6 is 2. Dividing both by 2 gives the simplified fraction 2/3.

This calculator implements these standard arithmetic rules to provide accurate fraction calculations.

function gcd(a, b) { var a = Math.abs(a); var b = Math.abs(b); while (b) { var temp = b; b = a % b; a = temp; } return a; } function simplifyFraction(num, den) { if (den === 0) return { num: num, den: den }; // Avoid division by zero var commonDivisor = gcd(num, den); num = num / commonDivisor; den = den / commonDivisor; // Ensure denominator is positive if (den < 0) { num = -num; den = -den; } return { num: num, den: den }; } function calculateFraction() { 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 operation = document.getElementById("operation").value; var resultDiv = document.getElementById("result"); // Input validation if (isNaN(num1) || isNaN(den1) || isNaN(num2) || isNaN(den2)) { resultDiv.textContent = "Please enter valid whole numbers."; resultDiv.style.backgroundColor = "#f8d7da"; // Error color resultDiv.style.color = "#721c24"; return; } if (den1 === 0 || den2 === 0) { resultDiv.textContent = "Denominator cannot be zero."; resultDiv.style.backgroundColor = "#f8d7da"; // Error color resultDiv.style.color = "#721c24"; return; } var resultNum, resultDen; if (operation === "add") { resultNum = (num1 * den2) + (num2 * den1); resultDen = den1 * den2; } else if (operation === "subtract") { resultNum = (num1 * den2) – (num2 * den1); resultDen = den1 * den2; } else if (operation === "multiply") { resultNum = num1 * num2; resultDen = den1 * den2; } else if (operation === "divide") { if (num2 === 0) { resultDiv.textContent = "Cannot divide by zero."; resultDiv.style.backgroundColor = "#f8d7da"; // Error color resultDiv.style.color = "#721c24"; return; } resultNum = num1 * den2; resultDen = den1 * num2; } var simplified = simplifyFraction(resultNum, resultDen); if (simplified.den === 0) { resultDiv.textContent = "Division by zero error in calculation."; resultDiv.style.backgroundColor = "#f8d7da"; // Error color resultDiv.style.color = "#721c24"; } else if (simplified.den === 1) { resultDiv.textContent = simplified.num; resultDiv.style.backgroundColor = "var(–success-green)"; // Success color resultDiv.style.color = "white"; } else { resultDiv.textContent = simplified.num + "/" + simplified.den; resultDiv.style.backgroundColor = "var(–success-green)"; // Success color resultDiv.style.color = "white"; } }

Leave a Comment