Calculate Simple Interest in JavaScript
Calculate simple interest in JavaScript with SI = P × R × T / 100. Learn why rate 5 means 5 percent and return an object, with explanation and output.
1// Formula: SI = (P × R × T) / 1002// Where: P = Principal, R = Rate, T = Time34let principal = 10000;5let rate = 5; // 5% per annum6let time = 2; // 2 years78let simpleInterest = (principal * rate * time) / 100;9let amount = principal + simpleInterest;1011console.log("Principal: ₹" + principal);12console.log("Rate: " + rate + "% per annum");13console.log("Time: " + time + " years");14console.log("Simple Interest: ₹" + simpleInterest);15console.log("Total Amount: ₹" + amount);1617// Function version18function calculateSimpleInterest(p, r, t) {19 let interest = (p * r * t) / 100;20 let total = p + interest;21 return {22 principal: p,23 rate: r,24 time: t,25 interest: interest,26 totalAmount: total27 };28}2930let result = calculateSimpleInterest(5000, 8, 3);31console.log("\nUsing function:");32console.log(`Principal: ₹${result.principal}`);33console.log(`Interest: ₹${result.interest.toFixed(2)}`);34console.log(`Total: ₹${result.totalAmount.toFixed(2)}`);
Sample Output
Principal: ₹10000 Rate: 5% per annum Time: 2 years Simple Interest: ₹1000 Total Amount: ₹11000 Using function: Principal: ₹5000 Interest: ₹1200.00 Total: ₹6200.00
In this one
- How to write the simple interest formula in code
- Why the rate is a percent, not a decimal
- How a function can return several values in an object
- How toFixed(2) formats money
How it works
- 1
principal, rate and time are set to 10000, 5 and 2.
- 2
Interest is (10000 * 5 * 2) / 100 = 1000.
- 3
Amount is 10000 + 1000 = 11000.
- 4
calculateSimpleInterest(5000, 8, 3) returns an object.
- 5
The object values are printed with two decimals.
Edge cases
Rate as a decimal
rate = 0.05 gives ₹10 interest instead of ₹1000.
This formula expects a percent. Use 5, or remove the / 100 if you store rates as decimals.
Time in months
6 months entered as 6 gives six years of interest.
Convert months to years first: 6 / 12 = 0.5.
String inputs
principal + interest can become "100001000".
If values come from inputs, convert them with Number() before adding.
What's going on
This program calculates simple interest. The formula is SI = (P × R × T) / 100. P is the principal, the money you start with. R is the yearly rate in percent. T is the time in years. Banks, loan apps and school maths questions all use this.
The first part uses ₹10000 at 5% for 2 years. 10000 × 5 × 2 is 100000. Divide by 100 and the interest is ₹1000. The total amount is principal plus interest, ₹11000.
Enter the rate as 5, not 0.05. The formula already divides by 100. If you type 0.05, you divide twice and the interest is 100 times too small. A quick test: ₹1000 at 5% for 2 years must give ₹100 interest. If you get ₹1, you treated 5 as 0.05.
The second part puts the formula in a function called calculateSimpleInterest. It returns an object with the principal, rate, time, interest and total. An object groups related values under names. The caller can read result.interest or result.totalAmount.
With ₹5000 at 8% for 3 years, the interest is ₹1200 and the total is ₹6200. toFixed(2) shows them as money with two decimal places.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Simple interest on ₹2000 at 10% for 1 year?
Pick an answer to check it.
Question 2
What should you pass as the rate for 7%?
Pick an answer to check it.
Questions people ask
What is the difference between simple and compound interest?
Simple interest is only on the principal. Compound interest also earns interest on past interest.
Why return an object?
One function call gives all the useful values together, with clear names.
Why does the first part print 1000 but the second 1200.00?
Only the second part uses toFixed(2).