Compound Interest Calculator
Compound interest is the interest calculated on the initial principal, which also includes all of the accumulated interest from previous periods on a deposit or loan. It is the eighth wonder of the world. He who understands it, earns it … he who doesn't … pays it.
The formula for compound interest is:
A = P (1 + r/n)^(nt)
Where:
- A = the future value of the investment/loan, including interest
- P = the principal investment amount (the initial deposit or loan amount)
- r = the annual interest rate (as a decimal)
- n = the number of times that interest is compounded per year
- t = the number of years the money is invested or borrowed for
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var years = parseFloat(document.getElementById("years").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(principal) || isNaN(annualRate) || isNaN(compoundingFrequency) || isNaN(years)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualRate < 0 || years < 0) {
resultDiv.innerHTML = "Principal must be positive, and rate/years cannot be negative.";
return;
}
var rateDecimal = annualRate / 100;
var exponent = compoundingFrequency * years;
var base = 1 + (rateDecimal / compoundingFrequency);
var futureValue = principal * Math.pow(base, exponent);
var totalInterest = futureValue – principal;
resultDiv.innerHTML = "
Calculation Results
" +
"Principal Amount: $" + principal.toFixed(2) + "" +
"Annual Interest Rate: " + annualRate.toFixed(2) + "%" +
"Compounding Frequency: " + getFrequencyDescription(compoundingFrequency) + "" +
"Number of Years: " + years.toFixed(0) + "" +
"
Future Value: $" + futureValue.toFixed(2) + "" +
"
Total Compound Interest Earned: $" + totalInterest.toFixed(2) + "";
}
function getFrequencyDescription(frequency) {
switch(frequency) {
case 1: return "Annually";
case 2: return "Semi-annually";
case 4: return "Quarterly";
case 12: return "Monthly";
case 365: return "Daily";
default: return "Custom";
}
}
.calculator-wrapper {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-wrapper h2, .calculator-wrapper h3 {
text-align: center;
color: #333;
}
.calculator-wrapper p {
margin-bottom: 15px;
line-height: 1.6;
}
.calculator-wrapper ul {
margin-left: 20px;
margin-bottom: 15px;
}
.calculator-wrapper li {
margin-bottom: 5px;
}
.calculator-form {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
}
.form-field {
display: flex;
flex-direction: column;
}
.form-field label {
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.form-field input[type="number"],
.form-field select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.calculator-form button {
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #0056b3;
}
#result {
margin-top: 25px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 5px;
background-color: #fff;
text-align: center;
}
#result p {
margin-bottom: 8px;
}
#result strong {
color: #28a745;
}