Compound Interest Calculator
Compound interest is a powerful concept in finance. It's often described as "interest on interest." Unlike simple interest, where interest is calculated only on the initial principal amount, compound interest calculates interest on the principal *and* on any accumulated interest from previous periods. This means your money grows at an accelerating rate over time, making it a key driver for long-term savings and investments.
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
This calculator will help you visualize how your investment can grow with compound interest.
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var compoundingFrequency = parseFloat(document.getElementById("compoundingFrequency").value);
var years = parseFloat(document.getElementById("years").value);
var resultElement = document.getElementById("result");
resultElement.innerHTML = ""; // Clear previous results
if (isNaN(principal) || isNaN(annualRate) || isNaN(compoundingFrequency) || isNaN(years)) {
resultElement.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualRate < 0 || compoundingFrequency <= 0 || years <= 0) {
resultElement.innerHTML = "Please enter positive values for all fields, with a rate greater than or equal to 0.";
return;
}
var rateDecimal = annualRate / 100;
var totalInterest = principal * Math.pow((1 + rateDecimal / compoundingFrequency), compoundingFrequency * years);
var totalAmount = totalInterest.toFixed(2);
var interestEarned = (totalInterest – principal).toFixed(2);
resultElement.innerHTML = "
Calculation Results
" +
"
Initial Investment: $" + principal.toFixed(2) + "" +
"
Annual Interest Rate: " + annualRate.toFixed(1) + "%" +
"
Compounding Frequency: " + getCompoundingFrequencyText(compoundingFrequency) + "" +
"
Investment Period: " + years + " years" +
"
Total Amount After " + years + " Years: $" + totalAmount + "" +
"
Total Interest Earned: $" + interestEarned + "";
}
function getCompoundingFrequencyText(frequency) {
switch(frequency) {
case 1: return "Annually";
case 2: return "Semi-Annually";
case 4: return "Quarterly";
case 12: return "Monthly";
case 52: return "Weekly";
case 365: return "Daily";
default: return frequency + " times per year";
}
}
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #e0e0e0;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
line-height: 1.6;
color: #555;
margin-bottom: 15px;
}
.calculator-container ul {
margin-left: 20px;
margin-bottom: 15px;
}
.calculator-container li {
margin-bottom: 5px;
}
.calculator-container strong {
font-weight: bold;
}
.calculator-inputs {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="number"],
.input-group select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.calculator-container button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1em;
transition: background-color 0.3s ease;
width: 100%;
}
.calculator-container button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
background-color: #f9f9f9;
border: 1px solid #eee;
border-radius: 4px;
}
.calculator-result h3 {
margin-top: 0;
color: #333;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
margin-bottom: 15px;
}
.calculator-result p {
margin-bottom: 10px;
color: #555;
}
.calculator-result strong.highlight {
color: #28a745;
font-size: 1.2em;
}