Calculate Simple Interest
Program to calculate simple interest
JavaScript Calculate Simple Interest Program
This program helps you to learn the fundamental structure and syntax of JavaScript programming.
// Formula: SI = (P × R × T) / 100
// Where: P = Principal, R = Rate, T = Time
let principal = 10000;
let rate = 5; // 5% per annum
let time = 2; // 2 years
let simpleInterest = (principal * rate * time) / 100;
let amount = principal + simpleInterest;
console.log("Principal: ₹" + principal);
console.log("Rate: " + rate + "% per annum");
console.log("Time: " + time + " years");
console.log("Simple Interest: ₹" + simpleInterest);
console.log("Total Amount: ₹" + amount);
// Function version
function calculateSimpleInterest(p, r, t) {
let interest = (p * r * t) / 100;
let total = p + interest;
return {
principal: p,
rate: r,
time: t,
interest: interest,
totalAmount: total
};
}
let result = calculateSimpleInterest(5000, 8, 3);
console.log("\nUsing function:");
console.log(`Principal: ₹${result.principal}`);
console.log(`Interest: ₹${result.interest.toFixed(2)}`);
console.log(`Total: ₹${result.totalAmount.toFixed(2)}`);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
Understanding Calculate Simple Interest
This program calculates simple interest using the standard formula.
Simple Interest Formula
SI = (P × R × T) / 100
Where:
-
P
= Principal (initial amount)
-
R
= Rate of interest (percentage per year)
-
T
= Time period (in years)
Total Amount
Amount = Principal + Simple Interest
Example Calculation
For ₹10,000 at 5% for 2 years:
Key Concepts
1.
Principal
: The initial amount invested/borrowed
2.
Rate
: Interest rate as percentage (e.g., 5 means 5%)
3.
Time
: Duration in years
4.
Interest
: The extra amount earned/paid
5.
Amount
: Principal + Interest
Function Approach
Creating a reusable function that returns an object:
function calculateSimpleInterest(p, r, t) {
let interest = (p * r * t) / 100;
principal: p,
interest: interest,
totalAmount: p + interest
};
}
return {Benefits:
Real-world Applications
Note:
Simple interest is different from compound interest, which calculates interest on interest!
Let us now understand every line and the components of the above program.
Note: To write and run JavaScript programs, you need to set up the local environment on your computer. Refer to the complete article Setting up JavaScript Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your JavaScript programs.
Practical Learning Notes for Calculate Simple Interest
This JavaScript program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.