Fraction Calculator
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7f6;
color: #333;
line-height: 1.6;
margin: 0;
padding: 20px;
display: flex;
justify-content: center;
align-items: flex-start; /* Align items to the top */
min-height: 100vh;
}
.fraction-calc-container {
background-color: #ffffff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
width: 100%;
max-width: 700px;
margin-top: 20px;
margin-bottom: 40px;
}
h1, h2 {
color: #004a99;
text-align: center;
margin-bottom: 25px;
}
.input-group {
margin-bottom: 20px;
display: flex;
align-items: center;
gap: 15px;
flex-wrap: wrap; /* Allow wrapping on smaller screens */
}
.input-group label {
flex: 1 1 120px; /* Grow, shrink, basis */
font-weight: 600;
color: #004a99;
text-align: right;
}
.fraction-inputs {
display: flex;
gap: 5px;
align-items: center;
flex-wrap: wrap;
}
.fraction-inputs input[type="number"] {
width: 60px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
text-align: center;
font-size: 1rem;
box-sizing: border-box;
}
.fraction-inputs .separator {
font-size: 1.2rem;
font-weight: bold;
color: #555;
}
.operation-select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
background-color: #e9ecef;
min-width: 150px;
margin: 0 10px;
}
button {
background-color: #004a99;
color: white;
border: none;
padding: 12px 25px;
border-radius: 5px;
cursor: pointer;
font-size: 1.1rem;
transition: background-color 0.3s ease;
width: 100%;
max-width: 200px; /* Limit button width */
margin: 20px auto 0 auto; /* Center the button */
display: block; /* Ensure it's a block element for margin auto centering */
}
button:hover {
background-color: #003366;
}
#result {
background-color: #28a745;
color: white;
padding: 20px;
margin-top: 30px;
border-radius: 5px;
text-align: center;
font-size: 1.5rem;
font-weight: bold;
min-height: 50px; /* Ensure it has some height */
display: flex;
justify-content: center;
align-items: center;
}
.explanation {
margin-top: 40px;
padding: 25px;
background-color: #e9ecef;
border-radius: 8px;
}
.explanation h2 {
color: #004a99;
margin-bottom: 20px;
}
.explanation p, .explanation ul {
margin-bottom: 15px;
}
.explanation li {
margin-bottom: 8px;
}
.explanation strong {
color: #004a99;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.input-group {
flex-direction: column;
align-items: center;
gap: 10px;
}
.input-group label {
text-align: center;
margin-bottom: 5px;
}
.fraction-inputs {
justify-content: center;
width: 100%;
}
.fraction-inputs input[type="number"] {
width: 50px;
padding: 8px;
}
.operation-select {
width: 80%;
margin: 10px 0;
}
button {
width: 90%;
max-width: none;
}
#result {
font-size: 1.3rem;
}
}
Fraction Calculator
Understanding Fraction Calculations
Fractions represent a part of a whole. They consist of a numerator (the top number) and a denominator (the bottom number). Calculating with fractions is a fundamental skill in mathematics with applications in cooking, engineering, finance, and everyday problem-solving.
Common Fraction Operations:
-
Addition and Subtraction: To add or subtract fractions, they must have a common denominator.
- If denominators are the same (e.g., 1/4 + 2/4), add/subtract the numerators and keep the denominator: (1+2)/4 = 3/4.
- If denominators are different (e.g., 1/2 + 1/3), find the least common multiple (LCM) of the denominators. Convert each fraction to an equivalent fraction with the LCM as the new denominator. Then, add or subtract the numerators: LCM of 2 and 3 is 6. (1/2 * 3/3) + (1/3 * 2/2) = 3/6 + 2/6 = 5/6.
-
Multiplication: To multiply fractions, multiply the numerators together and multiply the denominators together. Simplification can often be done before or after multiplication.
- Example: 2/3 * 1/4 = (2*1) / (3*4) = 2/12. This can be simplified to 1/6.
-
Division: To divide fractions, multiply the first fraction by the reciprocal (inverse) of the second fraction. The reciprocal of a fraction is obtained by flipping the numerator and denominator.
- Example: 1/2 รท 1/3 = 1/2 * 3/1 = (1*3) / (2*1) = 3/2.
Simplification (Reducing Fractions):
After performing an operation, it's good practice to simplify the resulting fraction to its lowest terms. This is done by dividing both the numerator and the denominator by their greatest common divisor (GCD).
For example, in 6/8, the GCD of 6 and 8 is 2. Dividing both by 2 gives 3/4.
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;
}
function calculateFraction() {
var num1 = parseFloat(document.getElementById("num1").value);
var den1 = parseFloat(document.getElementById("den1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var den2 = parseFloat(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.innerText = "Error: Please enter valid numbers.";
return;
}
if (den1 === 0 || den2 === 0) {
resultDiv.innerText = "Error: Denominator cannot be zero.";
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") {
resultNum = num1 * den2;
resultDen = den1 * num2;
}
// Handle potential division by zero after operation
if (resultDen === 0) {
resultDiv.innerText = "Error: Resulting denominator is zero.";
return;
}
// Simplify the fraction
var commonDivisor = gcd(resultNum, resultDen);
var simplifiedNum = resultNum / commonDivisor;
var simplifiedDen = resultDen / commonDivisor;
// Format the output
var resultText = "";
if (simplifiedDen === 1) {
resultText = simplifiedNum.toString(); // Display as whole number if denominator is 1
} else {
// Handle negative denominator by moving the sign to the numerator
if (simplifiedDen < 0) {
simplifiedNum = -simplifiedNum;
simplifiedDen = -simplifiedDen;
}
resultText = simplifiedNum + " / " + simplifiedDen;
}
resultDiv.innerText = "Result: " + resultText;
}