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." 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
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var compoundingFrequency = parseInt(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)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualRate < 0 || compoundingFrequency <= 0 || years <= 0) {
resultDiv.innerHTML = "Please enter positive values for principal, compounding frequency, and years, and a non-negative value for the annual rate.";
return;
}
var rateDecimal = annualRate / 100;
var totalInterest = principal * Math.pow((1 + rateDecimal / compoundingFrequency), (compoundingFrequency * years)) – principal;
var futureValue = principal + totalInterest;
resultDiv.innerHTML = "
Results:
";
resultDiv.innerHTML += "
Initial Principal: $" + principal.toFixed(2) + "";
resultDiv.innerHTML += "
Annual Interest Rate: " + annualRate.toFixed(2) + "%";
resultDiv.innerHTML += "
Compounding Frequency: " + getFrequencyDescription(compoundingFrequency) + "";
resultDiv.innerHTML += "
Number of Years: " + years.toFixed(0) + "";
resultDiv.innerHTML += "
Total Compound Interest Earned: $" + totalInterest.toFixed(2) + "";
resultDiv.innerHTML += "
Future Value: $" + futureValue.toFixed(2) + "";
}
function getFrequencyDescription(frequency) {
switch (frequency) {
case 1: return "Annually";
case 2: return "Semi-Annually";
case 4: return "Quarterly";
case 12: return "Monthly";
case 365: return "Daily";
default: return "Unknown";
}
}
.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;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
color: #555;
line-height: 1.6;
}
.calculator-container ul {
margin-bottom: 20px;
padding-left: 20px;
}
.calculator-container li {
margin-bottom: 5px;
}
.calculator-form {
margin-top: 20px;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
color: #333;
}
.form-group input[type="number"],
.form-group select {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.calculator-form button {
grid-column: 1 / -1;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background-color: #fff;
}
.calculator-result h3 {
color: #007bff;
margin-bottom: 15px;
}
.calculator-result p {
margin-bottom: 8px;
color: #333;
}
.calculator-result strong {
color: #000;
}