0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Calculate Simple Interest with Input and Output

Use a Bash script with the formula simple_interest=$((principal * rate * time / 100)) to calculate simple interest by multiplying principal, rate, and time, then dividing by 100.
📋

Examples

Inputprincipal=1000, rate=5, time=2
OutputSimple Interest: 100
Inputprincipal=1500, rate=4, time=3
OutputSimple Interest: 180
Inputprincipal=0, rate=10, time=5
OutputSimple Interest: 0
🧠

How to Think About It

To calculate simple interest, multiply the principal amount by the rate of interest and the time period, then divide the result by 100. This formula gives the interest earned over the time period.
📐

Algorithm

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

Code

bash
#!/bin/bash
read -p "Enter principal amount: " principal
read -p "Enter rate of interest: " rate
read -p "Enter time period (years): " time
simple_interest=$((principal * rate * time / 100))
echo "Simple Interest: $simple_interest"
Output
Enter principal amount: 1000 Enter rate of interest: 5 Enter time period (years): 2 Simple Interest: 100
🔍

Dry Run

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

1

Input values

principal=1000, rate=5, time=2

2

Calculate simple interest

simple_interest = (1000 * 5 * 2) / 100 = 10000 / 100 = 100

3

Output result

Print 'Simple Interest: 100'

VariableValue
principal1000
rate5
time2
simple_interest100
💡

Why This Works

Step 1: Getting inputs

The script uses read to get principal, rate, and time from the user.

Step 2: Calculating interest

It multiplies principal, rate, and time, then divides by 100 using $(( )) for arithmetic.

Step 3: Displaying result

The script prints the calculated simple interest with echo.

🔄

Alternative Approaches

Using bc for floating point calculation
bash
#!/bin/bash
read -p "Enter principal amount: " principal
read -p "Enter rate of interest: " rate
read -p "Enter time period (years): " time
simple_interest=$(echo "scale=2; $principal * $rate * $time / 100" | bc)
echo "Simple Interest: $simple_interest"
This method supports decimal values for rate and time but requires bc installed.
Using awk for calculation
bash
#!/bin/bash
read -p "Enter principal amount: " principal
read -p "Enter rate of interest: " rate
read -p "Enter time period (years): " time
simple_interest=$(awk "BEGIN {print $principal * $rate * $time / 100}")
echo "Simple Interest: $simple_interest"
Awk allows floating point math without external tools but is less common in simple scripts.

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?

The integer arithmetic approach is fastest and simplest; using bc or awk adds overhead but supports decimals.

ApproachTimeSpaceBest For
Integer arithmetic with $(( ))O(1)O(1)Simple integer calculations
Using bc for floating pointO(1)O(1)Calculations needing decimals
Using awk for floating pointO(1)O(1)Floating point without bc dependency
💡
Use integer arithmetic in Bash for simple interest unless decimals are needed, then use bc or awk.
⚠️
Forgetting to divide by 100 after multiplying principal, rate, and time leads to incorrect interest calculation.