Free html code for fraction adding calculator -fraction adding calculator html code
We will try to give you a new code for every day .
Here is a code for Adding Fraction calculator convertor HTML code
<html lang="en">
<head>
<meta charset="UTF-8"></meta>
<meta content="width=device-width, initial-scale=1.0" name="viewport"></meta>
<title>Fraction Adding Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
</style>
</head>
<body>
<h2>Fraction Adding Calculator</h2>
<label for="fractionInput">Enter Fractions (e.g., 1/2, 3/4, 1/3):</label>
<input id="fractionInput" placeholder="Enter fractions" type="text" />
<button onclick="addFractions()">Add Fractions</button>
<p id="result"></p>
<script>
function addFractions() {
// Get the fraction input value
var fractionInput = document.getElementById("fractionInput").value;
// Split the input into individual fractions
var fractions = fractionInput.split(',');
// Initialize variables to store the sum
var sumNumerator = 0;
var sumDenominator = 1; // Initialize to 1 to represent the identity element for addition
// Iterate through each fraction
for (var i = 0; i < fractions.length; i++) {
// Extract numerator and denominator from the current fraction
var parts = fractions[i].split('/');
var numerator = parseInt(parts[0]);
var denominator = parseInt(parts[1]);
// Check if the denominator is not zero
if (denominator === 0) {
document.getElementById("result").innerHTML = "Denominator cannot be zero.";
return;
}
// Find a common denominator
var commonDenominator = findCommonDenominator(sumDenominator, denominator);
// Calculate the sum
sumNumerator = sumNumerator * (commonDenominator / sumDenominator) +
numerator * (commonDenominator / denominator);
sumDenominator = commonDenominator;
}
// Simplify the result
var gcd = findGCD(sumNumerator, sumDenominator);
var simplifiedNumerator = sumNumerator / gcd;
var simplifiedDenominator = sumDenominator / gcd;
// Display the result
document.getElementById("result").innerHTML = "Sum: " + simplifiedNumerator + "/" + simplifiedDenominator;
}
function findCommonDenominator(a, b) {
// Find the least common multiple (LCM) as the common denominator
return (a * b) / findGCD(a, b);
}
function findGCD(a, b) {
// Find the Greatest Common Divisor (GCD) using Euclidean algorithm
return b === 0 ? a : findGCD(b, a % b);
}
</script>
</body>
Comments
Post a Comment