Shell program to find the sum of n numbers

Program:

sum=0
i=1
echo "Enter the number of terms:"
read n
echo "Enter the numbers:"
while  [ $i -le $n ] 
do
  read a
  sum=`expr $a + $sum`
  i=`expr $i + 1`
done
echo "Sum is $sum"

Output:

chmod +x {filename.sh}
./filename.sh
Enter the number of terms:
5
Enter the numbers:
1
2
3
4
5
Sum is 15

Leave a comment