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 addition of interest to the principal sum of a loan or deposit, or in other words, interest on interest . Compound interest provides a way for an investment to grow at an accelerated rate over time.
Initial Investment (Principal):
Annual Interest Rate (%):
Compounding Frequency (per year):
Annually
Semi-annually
Quarterly
Monthly
Weekly
Daily
Number of Years:
Calculate Compound Interest
.calculator-container {
font-family: Arial, sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
margin-bottom: 20px;
line-height: 1.5;
color: #555;
}
.input-section label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #444;
}
.input-section input[type="number"],
.input-section select {
width: calc(100% – 22px);
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box; /* Ensures padding doesn't affect width */
}
.input-section select {
cursor: pointer;
}
.calculator-container button {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
font-size: 16px;
margin-top: 10px;
}
.calculator-container button:hover {
background-color: #45a049;
}
.result-section {
margin-top: 25px;
padding: 15px;
background-color: #e7f3fe;
border: 1px solid #b3d7ff;
border-radius: 5px;
text-align: center;
font-size: 1.1em;
color: #333;
}
.result-section strong {
color: #4CAF50;
}
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var time = parseFloat(document.getElementById("time").value);
// Input validation
if (isNaN(principal) || principal <= 0) {
document.getElementById("result").innerHTML = "Please enter a valid initial investment amount.";
return;
}
if (isNaN(annualRate) || annualRate < 0) {
document.getElementById("result").innerHTML = "Please enter a valid annual interest rate.";
return;
}
if (isNaN(compoundingFrequency) || compoundingFrequency <= 0) {
document.getElementById("result").innerHTML = "Please select a valid compounding frequency.";
return;
}
if (isNaN(time) || time <= 0) {
document.getElementById("result").innerHTML = "Please enter a valid number of years.";
return;
}
// Convert annual rate to decimal
var rateDecimal = annualRate / 100;
// Calculate the total number of compounding periods
var numberOfPeriods = compoundingFrequency * time;
// Calculate the interest rate per period
var ratePerPeriod = rateDecimal / compoundingFrequency;
// Compound Interest Formula: 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
var futureValue = principal * Math.pow(1 + ratePerPeriod, numberOfPeriods);
var totalInterestEarned = futureValue – principal;
document.getElementById("result").innerHTML =
"
Calculation Results: " +
"Initial Investment: $" + principal.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + "" +
"Annual Interest Rate: " + annualRate + "%" +
"Compounding Frequency: " + getFrequencyText(compoundingFrequency) + "" +
"Number of Years: " + time + "" +
"Total Amount After " + time + " Years:
$" + futureValue.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + " " +
"Total Interest Earned:
$" + totalInterestEarned.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) + " ";
}
function getFrequencyText(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 "Custom";
}
}