Compound Interest Calculator
function calculateCompoundInterest() {
var principal = parseFloat(document.getElementById("principal").value);
var annualRate = parseFloat(document.getElementById("annualRate").value);
var time = parseFloat(document.getElementById("time").value);
var compoundingFrequency = parseInt(document.getElementById("compoundingFrequency").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(principal) || isNaN(annualRate) || isNaN(time) || isNaN(compoundingFrequency)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (principal <= 0 || annualRate < 0 || time <= 0 || compoundingFrequency <= 0) {
resultDiv.innerHTML = "Please enter positive values for principal, time, and compounding frequency, and a non-negative interest rate.";
return;
}
var rate = annualRate / 100;
var amount = principal * Math.pow(1 + rate / compoundingFrequency, compoundingFrequency * time);
var interestEarned = amount – principal;
resultDiv.innerHTML = `
Initial Investment: $${principal.toFixed(2)}
Annual Interest Rate: ${annualRate.toFixed(2)}%
Time Period: ${time} years
Compounding Frequency: ${getCompoundingFrequencyText(compoundingFrequency)}
Total Amount After ${time} Years: $${amount.toFixed(2)}
Total Interest Earned: $${interestEarned.toFixed(2)}
`;
}
function getCompoundingFrequencyText(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-wrapper {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-inputs {
display: grid;
gap: 15px;
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px;
font-weight: bold;
}
.form-group input[type="number"],
.form-group select {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
}
.calculator-wrapper button {
background-color: #4CAF50;
color: white;
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 1.1rem;
transition: background-color 0.3s ease;
}
.calculator-wrapper button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 20px;
padding: 15px;
border: 1px solid #eee;
background-color: #fff;
border-radius: 4px;
}
.calculator-result p {
margin: 5px 0;
}
### Understanding Compound Interest
Compound interest is often called the "eighth wonder of the world" because of its power to grow wealth over time. Unlike simple interest, where interest is calculated only on the initial principal amount, compound interest is calculated on the initial principal *and* the accumulated interest from previous periods. This means your money grows at an accelerating rate as your earnings start to generate their own earnings.
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
The "compounding frequency" ($n$) is a crucial factor. The more frequently interest is compounded (e.g., daily vs. annually), the faster your money will grow, assuming all other factors remain the same.
### How the Compound Interest Calculator Works
This calculator helps you visualize the power of compounding. You input:
* **Initial Investment (Principal):** The starting amount of money you invest.
* **Annual Interest Rate (%):** The yearly percentage return you expect on your investment.
* **Time Period (Years):** How long you plan to keep your money invested.
* **Compounding Frequency:** How often the interest is calculated and added to your principal (Annually, Semi-Annually, Quarterly, Monthly, or Daily).
The calculator then uses the compound interest formula to determine the total amount you will have after the specified time period, as well as the total interest earned.
### Example Calculation
Let's say you invest an **Initial Investment** of **$5,000**. You expect an **Annual Interest Rate** of **7%**. You plan to leave the money invested for **20 years**, and the interest is compounded **monthly**.
Using the calculator:
* Principal ($P$): $5,000
* Annual Rate ($r$): 7% or 0.07
* Time ($t$): 20 years
* Compounding Frequency ($n$): 12 (monthly)
The calculation would be:
$A = 5000(1 + 0.07/12)^(12*20)$
$A = 5000(1 + 0.0058333)^(240)$
$A = 5000(1.0058333)^(240)$
$A \approx 5000 * 4.0387 \approx $20,193.54
The total interest earned would be $20,193.54 – $5,000 = $15,193.54.
This demonstrates how even a moderate interest rate can significantly multiply your initial investment over a long period, especially with frequent compounding.