Compound Interest Calculator
Compound interest is the interest calculated on the initial principal, which also includes all of the accumulated interest from previous periods. It's often referred to as "interest on interest." This means your money grows at an accelerating rate over time, making it a powerful tool for long-term wealth building.
The formula for compound interest is: 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
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 resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(principal) || isNaN(annualRate) || isNaN(compoundingFrequency) || isNaN(years) ||
principal <= 0 || annualRate <= 0 || compoundingFrequency <= 0 || years <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
var ratePerPeriod = annualRate / 100 / compoundingFrequency;
var numberOfPeriods = compoundingFrequency * years;
var futureValue = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
var totalInterestEarned = futureValue – principal;
resultDiv.innerHTML = "
Results
" +
"
Future Value: $" + futureValue.toFixed(2) + "" +
"
Total Interest Earned: $" + totalInterestEarned.toFixed(2) + "";
}
.calculator-container {
font-family: sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
margin-bottom: 15px;
color: #333;
}
.calculator-container p {
line-height: 1.6;
color: #555;
margin-bottom: 10px;
}
.calculator-container ul {
margin-left: 20px;
margin-bottom: 20px;
}
.calculator-container li {
margin-bottom: 5px;
color: #555;
}
.calculator-inputs {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 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 input[type="text"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.calculator-inputs button {
grid-column: 1 / -1; /* Span across all columns */
padding: 12px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-inputs button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 5px;
background-color: #fff;
text-align: center;
}
.calculator-result h3 {
margin-top: 0;
color: #333;
}
.calculator-result p {
margin-bottom: 8px;
color: #333;
font-size: 1.1em;
}
.calculator-result strong {
color: #0056b3;
}