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. Compound interest is interest on interest. If the interest is calculated with just the principal, it is called simple interest. Compound interest can be applied daily, monthly, quarterly, semi-annually, or annually.
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
Calculate
Results:
Total Amount:
Total Interest Earned:
.calculator-container {
font-family: sans-serif;
padding: 20px;
border: 1px solid #ccc;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2, .calculator-container h3 {
text-align: center;
color: #333;
}
.calculator-container p {
margin-bottom: 15px;
line-height: 1.6;
color: #555;
}
.calculator-container ul {
margin-left: 20px;
color: #555;
}
.calculator-inputs {
margin-top: 20px;
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 #ccc;
border-radius: 4px;
font-size: 1rem;
}
.calculator-container button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
cursor: pointer;
margin-top: 20px;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 30px;
padding: 15px;
background-color: #e9ecef;
border: 1px solid #dee2e6;
border-radius: 4px;
text-align: center;
}
.calculator-result p {
margin-bottom: 10px;
color: #333;
}
.calculator-result span {
font-weight: bold;
color: #007bff;
}
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");
var totalAmountSpan = document.getElementById("totalAmount");
var totalInterestSpan = document.getElementById("totalInterest");
// Validate inputs
if (isNaN(principal) || principal <= 0 ||
isNaN(annualRate) || annualRate <= 0 ||
isNaN(compoundingFrequency) || compoundingFrequency <= 0 ||
isNaN(years) || years <= 0) {
totalAmountSpan.textContent = "Invalid input. Please enter positive numbers.";
totalInterestSpan.textContent = "";
resultDiv.style.display = "block";
return;
}
var ratePerPeriod = (annualRate / 100) / compoundingFrequency;
var numberOfPeriods = compoundingFrequency * years;
var totalAmount = principal * Math.pow((1 + ratePerPeriod), numberOfPeriods);
var totalInterest = totalAmount – principal;
totalAmountSpan.textContent = "$" + totalAmount.toFixed(2);
totalInterestSpan.textContent = "$" + totalInterest.toFixed(2);
resultDiv.style.display = "block";
}