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.
Understanding compound interest is crucial for both investors and borrowers. For investors, it means your money grows exponentially over time, as your earnings start generating their own earnings. For borrowers, it means the amount you owe can grow significantly faster than with simple interest, especially over longer periods.
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
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-container p {
line-height: 1.6;
color: #555;
margin-bottom: 30px;
}
.calculator-form {
display: grid;
grid-template-columns: 1fr;
gap: 15px;
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.form-group input[type="number"],
.form-group select {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1em;
}
.calculator-form button {
background-color: #007bff;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background-color: #fff;
font-size: 1.1em;
text-align: center;
color: #333;
}
.calculator-result span {
font-weight: bold;
color: #28a745;
}
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);
if (isNaN(principal) || isNaN(annualRate) || isNaN(compoundingFrequency) || isNaN(years) || principal <= 0 || annualRate < 0 || compoundingFrequency <= 0 || years <= 0) {
document.getElementById("result").innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
var rate = annualRate / 100; // Convert percentage to decimal
var time = years;
var n = compoundingFrequency;
var p = principal;
var amount = p * Math.pow((1 + rate / n), (n * time));
var interestEarned = amount – p;
document.getElementById("result").innerHTML = "Future Value: $" + amount.toFixed(2) + "Total Interest Earned: $" + interestEarned.toFixed(2);
}