Bash Script to Find Area of Triangle with Input and Output
Use a Bash script that reads base and height, then calculates area with
area=$(echo "scale=2; 0.5 * $base * $height" | bc) and prints the result.Examples
Inputbase=10, height=5
OutputArea of the triangle is 25.00
Inputbase=3, height=4
OutputArea of the triangle is 6.00
Inputbase=0, height=10
OutputArea of the triangle is 0.00
How to Think About It
To find the area of a triangle, you multiply the base by the height and then divide by 2. In Bash, since it does not support floating-point math directly, you use the
bc command to perform the calculation.Algorithm
1
Get the base value from the user2
Get the height value from the user3
Calculate the area using the formula (0.5 * base * height)4
Print the calculated areaCode
bash
#!/bin/bash read -p "Enter base of the triangle: " base read -p "Enter height of the triangle: " height area=$(echo "scale=2; 0.5 * $base * $height" | bc) echo "Area of the triangle is $area"
Output
Enter base of the triangle: 10
Enter height of the triangle: 5
Area of the triangle is 25.00
Dry Run
Let's trace the example where base=10 and height=5 through the code
1
Read base
User inputs base=10
2
Read height
User inputs height=5
3
Calculate area
Calculate 0.5 * 10 * 5 = 25.00 using bc
| Variable | Value |
|---|---|
| base | 10 |
| height | 5 |
| area | 25.00 |
Why This Works
Step 1: Reading inputs
The script uses read to get base and height values from the user.
Step 2: Calculating area
Since Bash can't do floating math, it uses bc to compute 0.5 * base * height with two decimal places.
Step 3: Displaying result
The script prints the area with a clear message using echo.
Alternative Approaches
Using awk for calculation
bash
#!/bin/bash read -p "Enter base: " base read -p "Enter height: " height area=$(awk "BEGIN {print 0.5 * $base * $height}") echo "Area of the triangle is $area"
This uses awk for floating-point math instead of bc; simpler but requires awk installed.
Using integer math approximation
bash
#!/bin/bash read -p "Enter base (integer): " base read -p "Enter height (integer): " height area=$((base * height / 2)) echo "Approximate area of the triangle is $area"
This uses integer math only, so no decimals; simpler but less precise.
Complexity: O(1) time, O(1) space
Time Complexity
The script performs a fixed number of operations regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables to store inputs and result, so space usage is constant O(1).
Which Approach is Fastest?
Using integer math is fastest but less accurate; bc and awk provide floating-point precision with minimal overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using bc | O(1) | O(1) | Accurate floating-point calculations |
| Using awk | O(1) | O(1) | Simple floating-point math without bc |
| Integer math | O(1) | O(1) | Fastest but only integer results |
Use
bc or awk for floating-point math in Bash scripts.Trying to do floating-point math directly in Bash without using external tools like bc or awk.