In this article, we will solve VNSGU TYBCA Semester 5 Linux (UNIX) Practical – Set A using shell scripting.
We will calculate:
- Simple Interest (SI)
- Compound Interest (CI)
- Total Amount
We will also see different approaches, including solutions with bc and without bc.
📌 Practical Question
Write a shell script to calculate the Simple Interest and Compound Interest for given amount.
📐 Formulas
Simple Interest
SI = (P × R × T) / 100
Where:
-
P= Principal Amount -
R= Rate of Interest -
T= Time
Simple Interest Amount
Amount = P + SI
Compound Interest
For annual compounding:
CI = P × (1 + R/100)^T - P
Compound Amount
CA = P × (1 + R/100)^T
✅ Solution 1: Using bc
bc is a command-line calculator available on Linux/UNIX systems.
It is useful when we need decimal calculations in shell scripts.
Shell Script
#!/bin/bash
echo "Enter Principal Amount:"
read P
echo "Enter Rate of Interest (%):"
read R
echo "Enter Time (in years):"
read T
SI=$(echo "scale=2; ($P * $R * $T) / 100" | bc)
CA=$(echo "scale=2; $P * (1 + $R / 100)^$T" | bc)
CI=$(echo "scale=2; $CA - $P" | bc)
echo "-----------------------------"
echo "Principal Amount : $P"
echo "Rate of Interest : $R%"
echo "Time : $T years"
echo "Simple Interest : $SI"
echo "Compound Interest: $CI"
echo "Compound Amount : $CA"
echo "-----------------------------"
Example Output
Enter Principal Amount:
10000
Enter Rate of Interest (%):
10
Enter Time (in years):
2
-----------------------------
Principal Amount : 10000
Rate of Interest : 10%
Time : 2 years
Simple Interest : 2000.00
Compound Interest: 2100.00
Compound Amount : 12100.00
-----------------------------
How it works
SI=$(echo "scale=2; ($P * $R * $T) / 100" | bc)
Here, bc performs the decimal calculation.
scale=2
means that the result can contain 2 digits after the decimal point.
For compound interest:
CA=$(echo "scale=2; $P * (1 + $R / 100)^$T" | bc)
The ^ operator is used for exponentiation.
✅ Solution 2: Without bc
Bash's normal arithmetic expansion:
$(( ... ))
works with integers, not floating-point numbers.
Therefore, if the practical only requires integer input and integer output, we can calculate SI and CI using Bash arithmetic.
Shell Script
#!/bin/bash
echo "Enter Principal Amount:"
read P
echo "Enter Rate of Interest (%):"
read R
echo "Enter Time (in years):"
read T
SI=$((P * R * T / 100))
AMOUNT=$P
for ((i=1; i<=T; i++))
do
AMOUNT=$((AMOUNT * (100 + R) / 100))
done
CI=$((AMOUNT - P))
echo "-----------------------------"
echo "Principal Amount : $P"
echo "Rate of Interest : $R%"
echo "Time : $T years"
echo "Simple Interest : $SI"
echo "Compound Interest: $CI"
echo "Compound Amount : $AMOUNT"
echo "-----------------------------"
Example
For:
P = 10000
R = 10
T = 2
Output:
Simple Interest : 2000
Compound Interest: 2100
Compound Amount : 12100
Important Note
This method uses Bash integer arithmetic.
For example:
$((10 / 3))
produces:
3
not:
3.3333
So this method is suitable when decimal precision is not required.
✅ Solution 3: Using awk
If you don't want to use bc but still want decimal calculations, awk is a very good alternative.
Shell Script
#!/bin/bash
echo "Enter Principal Amount:"
read P
echo "Enter Rate of Interest (%):"
read R
echo "Enter Time (in years):"
read T
awk -v P="$P" -v R="$R" -v T="$T" '
BEGIN {
SI = (P * R * T) / 100
CA = P * (1 + R / 100)^T
CI = CA - P
printf "-----------------------------\n"
printf "Principal Amount : %.2f\n", P
printf "Rate of Interest : %.2f%%\n", R
printf "Time : %d years\n", T
printf "Simple Interest : %.2f\n", SI
printf "Compound Interest: %.2f\n", CI
printf "Compound Amount : %.2f\n", CA
printf "-----------------------------\n"
}'
Example Output
Enter Principal Amount:
10000
Enter Rate of Interest (%):
10
Enter Time (in years):
2
-----------------------------
Principal Amount : 10000.00
Rate of Interest : 10.00%
Time : 2 years
Simple Interest : 2000.00
Compound Interest: 2100.00
Compound Amount : 12100.00
-----------------------------
🔍 Difference Between the Solutions
| Method | Decimal Support | Easy to Understand | External Command |
|---|---|---|---|
bc |
✅ Yes | ⭐⭐⭐ | bc |
| Bash arithmetic | ❌ Integer only | ⭐⭐⭐⭐ | No |
awk |
✅ Yes | ⭐⭐ | awk |
Which one should you use?
For a Linux practical examination:
If bc is allowed: use the bc solution.
If bc is not allowed: use the Bash arithmetic solution when integer output is acceptable.
If decimal output is required but bc is not allowed: use awk.
🧠 Important Shell Concepts Used
This practical helps you practice several important shell scripting concepts:
1. Taking Input
read P
2. Arithmetic Expansion
$(( ... ))
3. Command Substitution
SI=$(...)
4. bc
Used for floating-point calculations.
5. awk
Can perform floating-point calculations and formatted output.
6. for Loop
Used in the pure Bash compound-interest solution.
🎯 Practical Exam Tip
Don't just memorize the script.
Understand these three things:
1. How to take input
2. How to apply the SI and CI formulas
3. How to display the result
Once you understand these, you can easily modify the program if the examiner changes the question slightly.
📚 Related Article
This solution is part of the VNSGU TYBCA Sem 5 Linux (UNIX) Practical – OCT/Nov 2025 series.
You can also practice the other practical sets:
- Set A – Simple Interest & Compound Interest
- Set B – Vowels & Case Conversion
- Set C – Palindrome
- Set D – File Operations
- Set E – Display Lines with Validation
- Set F – Files & Directories
- Set G – Recently Modified Files
- Set H – Employee Gross Salary
⚠️ Note
The Compound Interest calculation in this article assumes annual compounding.
The Bash-only solution uses integer arithmetic, so decimal precision may differ from the bc or awk versions.
🐧 Conclusion
The Simple Interest and Compound Interest problem is a good beginner-level shell scripting practical because it combines user input, arithmetic operations, variables, loops, and command-line utilities.
Practice all three approaches so that you are comfortable solving the question even when the examination environment has different restrictions.
Happy Learning & Best of Luck for your Linux Practical! 🐧💻
Top comments (0)