diff --git a/Calculators/Percentage-To-Fraction-Calculator/DTI-percent-sign-calculator-red-symbols.jpg b/Calculators/Percentage-Fraction-Calculator/DTI-percent-sign-calculator-red-symbols.jpg similarity index 100% rename from Calculators/Percentage-To-Fraction-Calculator/DTI-percent-sign-calculator-red-symbols.jpg rename to Calculators/Percentage-Fraction-Calculator/DTI-percent-sign-calculator-red-symbols.jpg diff --git a/Calculators/Percentage-Fraction-Calculator/Readme.md b/Calculators/Percentage-Fraction-Calculator/Readme.md new file mode 100644 index 000000000..b146ee813 --- /dev/null +++ b/Calculators/Percentage-Fraction-Calculator/Readme.md @@ -0,0 +1,19 @@ +#

Percentage Fraction Calculator + + This is a simple online calculator which converts Percent to a Fraction and vice-versa. + +## Features :- + +- Converts Percent to Fraction or Fraction to Percent. +- It also provide detail solution. +- You can reset the calculation to starting. + +## Tech Stacks :- + +- HTML +- CSS +- JavaScript + +## Screenshots :- + +![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/3e9341d1-25c4-426e-b615-6f7ccaed2baa) diff --git a/Calculators/Percentage-Fraction-Calculator/index.html b/Calculators/Percentage-Fraction-Calculator/index.html new file mode 100644 index 000000000..be778eec1 --- /dev/null +++ b/Calculators/Percentage-Fraction-Calculator/index.html @@ -0,0 +1,46 @@ + + + + + + + + Percentage Fraction Calculator + + + + +

+

Percentage Fraction Calculator

+ + +
+ + + +
+ + +
+ + + +
+ + +
+

Answer:

+

+

Showing Work:

+

+
+ + + +
+ + + + + + \ No newline at end of file diff --git a/Calculators/Percentage-Fraction-Calculator/script.js b/Calculators/Percentage-Fraction-Calculator/script.js new file mode 100644 index 000000000..ace597717 --- /dev/null +++ b/Calculators/Percentage-Fraction-Calculator/script.js @@ -0,0 +1,105 @@ +window.convertPercentToFraction = function () { + var percentInput = document.getElementById('percentInput').value; + + // Check if the input is empty or not a valid number + if (percentInput.trim() === '' || isNaN(percentInput)) { + alert('Please enter a valid number.'); + return; + } + + // Convert percent to fraction + var decimalValue = parseFloat(percentInput) / 100; + var fraction = decimalToFraction(decimalValue); + + // Display the result + document.getElementById('resultValue').innerText = '= ' + fraction.numerator + '/' + fraction.denominator; + document.getElementById('showingWork').innerText = + percentInput + '% = ' + percentInput + '/100 = ' + decimalValue + ' = ' + fraction.numerator + '/' + fraction.denominator; +}; + +window.convertFractionToPercent = function () { + var fractionInput = document.getElementById('fractionInput').value; + + // Check if the input is empty or not a valid fraction + if (fractionInput.trim() === '' || !isValidFraction(fractionInput)) { + alert('Please enter a valid fraction.'); + return; + } + + // Split fraction into numerator and denominator + var parts = fractionInput.split('/'); + var numerator = parseInt(parts[0]); + var denominator = parseInt(parts[1]); + + // Check if denominator is zero + if (denominator === 0) { + alert('Denominator cannot be zero. Please enter a valid fraction.'); + return; + } + + // Calculate percentage + var percent = (numerator / denominator) * 100; + + // Simplify fraction + var gcd = findGCD(numerator, denominator); + var simplifiedNumerator = numerator / gcd; + var simplifiedDenominator = denominator / gcd; + + // Display the result + document.getElementById('resultValue').innerText = '= ' + percent.toFixed(2) + '%'; + + // Showing work steps + var workSteps = fractionInput + ' = (' + numerator + '/' + denominator + ') * 100'; + workSteps += ' = ' + percent.toFixed(2) + '%'; + + // Display the work steps + document.getElementById('showingWork').innerText = workSteps; +}; + +// Function to find the greatest common divisor (GCD) of two numbers +function findGCD(a, b) { + return b === 0 ? a : findGCD(b, a % b); +} + + +window.clearInputs = function () { + document.getElementById('percentInput').value = ''; + document.getElementById('fractionInput').value = ''; + document.getElementById('resultValue').innerText = ''; + document.getElementById('showingWork').innerText = ''; +}; + +// Function to convert decimal to fraction +function decimalToFraction(decimalValue) { + const tolerance = 1.0e-9; + + for (var denominator = 1; ; ++denominator) { + var numerator = Math.round(decimalValue * denominator); + + if (Math.abs(decimalValue - numerator / denominator) < tolerance) { + return { + numerator: numerator, + denominator: denominator, + }; + } + } +} + +// Function to check if the input is a valid fraction +function isValidFraction(input) { + return /^-?\d+\/\d+$/.test(input); +} + +// Function to convert fraction to percent +function fractionToPercent(fraction) { + var parts = fraction.split('/'); + var numerator = parseInt(parts[0]); + var denominator = parseInt(parts[1]); + + // Check if denominator is zero + if (denominator === 0) { + return NaN; // Return NaN to indicate invalid input + } + + return (numerator / denominator) * 100; +} diff --git a/Calculators/Percentage-To-Fraction-Calculator/style.css b/Calculators/Percentage-Fraction-Calculator/style.css similarity index 82% rename from Calculators/Percentage-To-Fraction-Calculator/style.css rename to Calculators/Percentage-Fraction-Calculator/style.css index 785d2d904..6e5a59ab0 100644 --- a/Calculators/Percentage-To-Fraction-Calculator/style.css +++ b/Calculators/Percentage-Fraction-Calculator/style.css @@ -1,86 +1,111 @@ -* { - margin: 0; - padding: 0; - box-sizing: border-box; -} - -/* Body styles with background image */ -body { - font-family: 'Arial', sans-serif; - background: url('DTI-percent-sign-calculator-red-symbols.jpg') center/cover fixed; - /* Add your background image URL here */ - margin: 0; - padding: 0; - display: flex; - align-items: center; - justify-content: center; - height: 100vh; -} - -/* Calculator styles */ -.calculator { - width: 100%; - max-width: 500px; - padding: 20px; - background-color: rgba(146, 206, 229, 0.8); - /* Semi-transparent white background */ - border-radius: 8px; - box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); - text-align: center; -} - -h1 { - text-align: center; - margin-bottom: 20px; -} - -label { - display: block; - margin-bottom: 5px; -} - -input[type="text"] { - width: 100%; - padding: 10px; - margin-bottom: 10px; - border: 1px solid #ccc; - font-size: 16px; - color: #555; -} - -button { - cursor: pointer; - padding: 10px 20px; - background-color: #337ab7; - color: #fff; - border: 1px solid #337ab7; - margin-right: 10px; - transition: background-color 0.3s ease; - /* Smooth transition on hover */ -} - -button:hover { - background-color: #49c36a; - /* Change both Convert and Clear hover color to red */ -} - -#result { - margin-top: 20px; - padding: 20px; - background-color: #fff; -} - -.answer { - font-weight: bold; - margin-bottom: 10px; -} - -.fraction { - font-size: 1.5rem; -} - -.showingWork { - font-style: italic; - margin-top: 12px; - margin-bottom: 15px; -} +* { + margin: 0; + padding: 0; + box-sizing: border-box; +} + +/* Body styles with background image */ +body { + font-family: 'Arial', sans-serif; + background: url('DTI-percent-sign-calculator-red-symbols.jpg') center/cover fixed; + /* Add your background image URL here */ + margin: 0; + padding: 0; + display: flex; + align-items: center; + justify-content: center; + height: 100vh; +} + +/* Calculator styles */ +.calculator { + width: 100%; + max-width: 500px; + padding: 20px; + background-color: rgba(146, 206, 229, 0.8); + /* Semi-transparent white background */ + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); + text-align: center; +} + +.inp { + margin-top: 20px; +} + +h1 { + text-align: center; + margin-bottom: 20px; +} + +label { + display: block; + margin-bottom: 5px; +} + +input[type="text"] { + width: 100%; + padding: 10px; + margin-bottom: 10px; + border: 1px solid #ccc; + font-size: 16px; + color: #555; +} + +button { + cursor: pointer; + padding: 10px 20px; + background-color: #337ab7; + color: #fff; + border: 1px solid #337ab7; + margin-right: 10px; + transition: background-color 0.3s ease; + /* Smooth transition on hover */ +} + +button:hover { + background-color: #49c36a; + /* Change both Convert and Clear hover color to red */ +} + +#clearButton { + margin-top: 10px; +} + +#result { + margin-top: 20px; + padding: 20px; + background-color: #fff; +} + +.answer { + font-weight: bold; + margin-bottom: 10px; +} + +.fraction { + font-size: 1.5rem; +} + +.showingWork { + font-style: italic; + margin-top: 12px; + margin-bottom: 15px; +} + +.showingWorkText { + font-style: italic; + margin-top: 12px; + margin-bottom: 15px; +} + +#percentResult { + font-weight: bold; + margin-top: 30px; +} + +#workStepsFraction { + font-style: italic; + margin-top: 15px; + margin-bottom: 20px; +} \ No newline at end of file diff --git a/Calculators/Percentage-To-Fraction-Calculator/Readme.md b/Calculators/Percentage-To-Fraction-Calculator/Readme.md deleted file mode 100644 index dd408dd5c..000000000 --- a/Calculators/Percentage-To-Fraction-Calculator/Readme.md +++ /dev/null @@ -1,25 +0,0 @@ -# Percentage To Fraction Calculator - - This is a simple online calculator converts a percent to a fraction - -## Features - -- Converts Percent to Fraction. -- It also provide detail solution -- Easy-to-use command-line interface. -- You can reset the calculation to starting - -## Example - -For example, to calculate the fraction of percentage 62.5%: -- Enter 62.5 -- Click "Convert" to see the result. - -## Tech Stack - -- HTML -- CSS -- JavaScript - -## Screenshots -![Alt text](image.png) diff --git a/Calculators/Percentage-To-Fraction-Calculator/image.png b/Calculators/Percentage-To-Fraction-Calculator/image.png deleted file mode 100644 index e02fc8d1b..000000000 Binary files a/Calculators/Percentage-To-Fraction-Calculator/image.png and /dev/null differ diff --git a/Calculators/Percentage-To-Fraction-Calculator/index.html b/Calculators/Percentage-To-Fraction-Calculator/index.html deleted file mode 100644 index 9d5625f06..000000000 --- a/Calculators/Percentage-To-Fraction-Calculator/index.html +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - Percentage To Fraction Calculator - - - -
-

Percentage To Fraction Calculator

- - - - -
-

Answer:

-

=

-

Showing Work:

-

-
-
- - - - diff --git a/Calculators/Percentage-To-Fraction-Calculator/script.js b/Calculators/Percentage-To-Fraction-Calculator/script.js deleted file mode 100644 index 894b9a221..000000000 --- a/Calculators/Percentage-To-Fraction-Calculator/script.js +++ /dev/null @@ -1,40 +0,0 @@ -window.convertPercentToFraction = function () { - var percentInput = document.getElementById('percentInput').value; - - // Check if the input is empty or not a valid number - if (percentInput.trim() === '' || isNaN(percentInput)) { - alert('Please enter a valid number.'); - return; - } - - // Convert percent to fraction - var decimalValue = parseFloat(percentInput) / 100; - var fraction = decimalToFraction(decimalValue); - - // Display the result - document.getElementById('fractionResult').innerText = '= ' + fraction.numerator + '/' + fraction.denominator; - document.getElementById('workSteps').innerText = - percentInput + '% = ' + percentInput + '/100 = ' + decimalValue + ' = ' + fraction.numerator + '/' + fraction.denominator; -}; - -window.clearInputs = function () { - document.getElementById('percentInput').value = ''; - document.getElementById('fractionResult').innerText = '='; - document.getElementById('workSteps').innerText = ''; -}; - -// Function to convert decimal to fraction -function decimalToFraction(decimalValue) { - const tolerance = 1.0e-9; - - for (var denominator = 1; ; ++denominator) { - var numerator = Math.round(decimalValue * denominator); - - if (Math.abs(decimalValue - numerator / denominator) < tolerance) { - return { - numerator: numerator, - denominator: denominator, - }; - } - } -} diff --git a/index.html b/index.html index 9f77d5415..766689b48 100644 --- a/index.html +++ b/index.html @@ -409,13 +409,13 @@

Calculates trigonometric functions and inverses.

-

Percentage To Fraction Calculator

-

Converts given Percentage to Fraction with detailed solution.

+

Percentage Fraction Calculator

+

Converts given Percentage to Fraction and vice-versa with detailed solution.