Skip to content

Commit

Permalink
Added mean function in Arithmetic Progression Calculator (#1791)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhumii-ka authored Aug 10, 2024
1 parent 25acc98 commit cee9826
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Calculators/Arithmetic-Progression-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description :-

Calculates nth Term and sum of n Terms present in an Arithematic Sequence.
Calculates nth Term, arithmetic mean and sum of n Terms present in an Arithematic Sequence.

## Tech Stacks :-

Expand Down
1 change: 1 addition & 0 deletions Calculators/Arithmetic-Progression-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ <h1>Arithmetic Progression Calculator</h1>
<select id="calculationType">
<option value="nthTerm">Nth Term</option>
<option value="sumOfTerms">Sum of First N Terms</option>
<option value="arithMean">Arithmetic Mean</option>
</select>

<button onclick="calculate()">Calculate</button>
Expand Down
13 changes: 13 additions & 0 deletions Calculators/Arithmetic-Progression-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ function calculate() {
const termNumber = parseInt(document.getElementById('termNumber').value);
const calculationType = document.getElementById('calculationType').value;

// For checking empty values
if (isNaN(firstTerm) || isNaN(commonDifference) || isNaN(termNumber)) {
document.getElementById("result").innerText = "Please enter the valid numbers for all fields.";
return;
}
// Perform the selected calculation
let result;
if (calculationType === 'nthTerm') {
Expand All @@ -13,6 +18,9 @@ function calculate() {
} else if (calculationType === 'sumOfTerms') {
// Calculate the sum of the first n terms
result = calculateSumOfTerms(firstTerm, commonDifference, termNumber);
} else if (calculationType === 'arithMean') {
// Calculate the arithmetic mean of terms
result = calculateArithMean(firstTerm, commonDifference, termNumber);
}

// Display the result
Expand All @@ -29,3 +37,8 @@ function calculateSumOfTerms(firstTerm, commonDifference, termNumber) {
// Calculate the sum of the first n terms
return `The sum of the first ${termNumber} terms is: ${(termNumber / 2) * (2 * firstTerm + (termNumber - 1) * commonDifference)}`;
}

function calculateArithMean(firstTerm, commonDifference, termNumber) {
// Calculate the arithmetic mean of terms
return `The arithmetic mean of ${termNumber} terms is: ${(1 / 2) * (2 * firstTerm + (termNumber - 1) * commonDifference)}`;
}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ <h3>Calculates nth Term and sum of n Terms of the Arithmetic Geometric Sequence.
<div class="box">
<div class="content">
<h2>Arithmetic Progression Calculator</h2>
<h3>Calculates nth Term and sum of n Terms present in an Arithmetic Sequence.</h3>
<h3>Calculates nth Term, arithmetic mean and sum of n Terms present in an Arithmetic Sequence.</h3>
<div class="card-footer">
<a href="./Calculators/Arithmetic-Progression-Calculator/index.html" target="_blank">
<button>Try Now</button>
Expand Down

0 comments on commit cee9826

Please sign in to comment.