0
0
PythonProgramBeginner · 2 min read

Python Program to Calculate Simple Interest

You can calculate simple interest in Python using the formula simple_interest = (principal * rate * time) / 100, where principal is the initial amount, rate is the interest rate, and time is the time period in years.
📋

Examples

Inputprincipal=1000, rate=5, time=2
OutputSimple Interest: 100.0
Inputprincipal=1500, rate=4.5, time=3
OutputSimple Interest: 202.5
Inputprincipal=0, rate=10, time=5
OutputSimple Interest: 0.0
🧠

How to Think About It

To calculate simple interest, first understand that it depends on three values: the principal amount, the rate of interest per year, and the time in years. Multiply these three values together and then divide by 100 to get the interest amount. This formula works like calculating a percentage of the principal over the given time.
📐

Algorithm

1
Get the principal amount from the user
2
Get the rate of interest from the user
3
Get the time period in years from the user
4
Calculate simple interest using the formula (principal * rate * time) / 100
5
Display the calculated simple interest
💻

Code

python
principal = float(input("Enter the principal amount: "))
rate = float(input("Enter the rate of interest: "))
time = float(input("Enter the time in years: "))
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest: {simple_interest}")
Output
Enter the principal amount: 1000 Enter the rate of interest: 5 Enter the time in years: 2 Simple Interest: 100.0
🔍

Dry Run

Let's trace the example where principal=1000, rate=5, and time=2 through the code

1

Input principal

principal = 1000.0

2

Input rate

rate = 5.0

3

Input time

time = 2.0

4

Calculate simple interest

simple_interest = (1000.0 * 5.0 * 2.0) / 100 = 100.0

5

Print result

Output: Simple Interest: 100.0

VariableValue
principal1000.0
rate5.0
time2.0
simple_interest100.0
💡

Why This Works

Step 1: Input values

The program asks the user to enter the principal, rate, and time, which are needed to calculate simple interest.

Step 2: Apply formula

It uses the formula (principal * rate * time) / 100 to find the interest earned over the time period.

Step 3: Display output

Finally, it prints the calculated simple interest so the user can see the result.

🔄

Alternative Approaches

Using a function
python
def calculate_simple_interest(p, r, t):
    return (p * r * t) / 100

principal = float(input("Enter principal: "))
rate = float(input("Enter rate: "))
time = float(input("Enter time: "))
interest = calculate_simple_interest(principal, rate, time)
print(f"Simple Interest: {interest}")
This approach organizes the calculation inside a function for reuse and clarity.
Using command line arguments
python
import sys
principal = float(sys.argv[1])
rate = float(sys.argv[2])
time = float(sys.argv[3])
simple_interest = (principal * rate * time) / 100
print(f"Simple Interest: {simple_interest}")
This method takes inputs from command line arguments, useful for automation but less interactive.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation involves a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used to store inputs and the result, so space usage is constant.

Which Approach is Fastest?

All approaches run in constant time and space; using a function improves code clarity but does not affect performance.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple scripts and quick calculations
Function-basedO(1)O(1)Reusable code and larger projects
Command line argumentsO(1)O(1)Automation and scripting without user interaction
💡
Always convert input values to float to handle decimal interest rates and amounts correctly.
⚠️
Forgetting to divide by 100 in the formula, which leads to incorrect interest calculation.