Question
Calculating geometrical properties of a cylinder: For a right cylinder with circular base radius r and height h, the volume is calculated as V = pi r^2 h and the entire surface area is computed using S = 2 pi r(h + r) Write a script that a) Asks the user for the values of the radius and height of an arbitrary cylinder. b) Asks the user if s/he wants to know the value of the volume or surface by inputting V or S. c) If either of r or h is zero or negative, it displays the warning message "The input cannot be zero or a negative number." d) It calculates the quantity requested by the user for the valid inputs. e) Based on the user request, it returns: "For the given (value of r in f format) and (value of h in f format), the volume is (value of V in f formal)." or "For the given (value of r in f format) and (value of h in f format), the surface is (value of S in f format)." f) Run your code for r = 1 and h = 1, S: g) Run your code for r = 1 and h = 2, V;
Explanation / Answer
#!/bin/bash
echo " Enter radius and height of a cylinder "
read r
read h
if [ $r -le 0 -o $h -le 0 ]
then
echo "The number cannot be negative or zero"
else
echo -n "Enter V to find Volume or S to find Surface area "
read n
if [ "$n" == "V" ]
then
volume=$(echo "scale=3; 3.14 * $r * $r * $h"|bc)
echo "Volume of cylinder is $volume"
elif [ "$n" == "S" ]
then
surface=$(echo "scale=3;2 * 3.14 * ($r * $r) + 2 * 3.14 *$r * $h"|bc)
echo "Surface Area of cylinder is $surface "
else
echo " Invalid entry! "
fi
fi