Shell Script to read the base and height of a traingle and find its area

in Categories Shell Math last updated April 3, 2008

Calculating the area of a triangle is an elementary problem encountered often in many different situations. The best known, and simplest formula is

Computing the area of a triangle

Where S is area, b is the length of the base of the triangle, and h is the height or altitude of the triangle. The term ‘base’ denotes any side, and ‘height’ denotes the length of a perpendicular from the point opposite the side onto the side itself.

# Shell program/script to read the base and height of a traingle and find its area
# -------------------------------------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# Formula info: http://www.mste.uiuc.edu/dildine/heron/triarea.html
 
# Area=(1/2) x Base x Height
echo -n "Enter base of a triangle : "
read b
 
echo -n "Enter height of a triangle : "
read h
# calculate it and display back
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
echo "Area of a triangle is $area"

Share this on:

Leave a Comment