Compound Interest Calculator
.calculator-container {
font-family: sans-serif;
max-width: 500px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-inputs {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
font-size: 0.9em;
color: #333;
}
.input-group input,
.input-group select {
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
}
.calculator-container button {
grid-column: 1 / -1;
padding: 10px 15px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
background-color: #e7f3ff;
border: 1px solid #b3d7ff;
border-radius: 4px;
font-size: 1.1em;
text-align: center;
font-weight: bold;
color: #0056b3;
}
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value) / 100; // Convert percentage to decimal
var time = parseFloat(document.getElementById("time").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var resultElement = document.getElementById("result");
// Validate inputs
if (isNaN(principal) || isNaN(annualRate) || isNaN(time) || isNaN(compoundingFrequency)) {
resultElement.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualRate < 0 || time <= 0 || compoundingFrequency <= 0) {
resultElement.innerHTML = "Please enter positive values for investment, time, and frequency, and a non-negative rate.";
return;
}
// Compound Interest Formula: A = P (1 + r/n)^(nt)
// 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
var amount = principal * Math.pow((1 + annualRate / compoundingFrequency), (compoundingFrequency * time));
var totalInterest = amount – principal;
resultElement.innerHTML = "Future Value: $" + amount.toFixed(2) + "Total Interest Earned: $" + totalInterest.toFixed(2);
}
Understanding Compound Interest
Compound interest is often called the "eighth wonder of the world." It's the interest earned not only on the initial principal amount but also on the accumulated interest from previous periods. This means your money grows at an accelerating rate over time, thanks to the power of earning interest on your interest.
The formula used to calculate compound interest is:
A = P (1 + r/n)^(nt)
Where:
- A is the future value of the investment/loan, including interest.
- P is the principal investment amount (the initial deposit or loan amount).
- r is the annual interest rate (expressed as a decimal). For example, 5% is 0.05.
- n is the number of times that interest is compounded per year. This can be annually (1), semi-annually (2), quarterly (4), monthly (12), or even daily (365).
- t is the number of years the money is invested or borrowed for.
How the Calculator Works:
- Initial Investment (Principal): This is the starting amount of money you put into the investment.
- Annual Interest Rate: This is the percentage return your investment is expected to earn each year.
- Investment Duration: The number of years you plan to keep your money invested.
- Compounding Frequency: This is crucial. The more frequently interest is compounded (e.g., daily vs. annually), the faster your money will grow, assuming the same annual rate.
Example:
Let's say you invest $1,000 (Principal) for 10 years (Time) at an 8% annual interest rate (Annual Rate), compounded monthly (Compounding Frequency = 12).
- P = 1000
- r = 0.08
- n = 12
- t = 10
Plugging these into the formula:
A = 1000 * (1 + 0.08/12)^(12*10)
A = 1000 * (1 + 0.0066667)^(120)
A = 1000 * (1.0066667)^120
A = 1000 * 2.21964
A ≈ $2,219.64
The total interest earned would be $2,219.64 – $1,000 = $1,219.64. This demonstrates the significant impact of compounding over time.