Monday 9 July 2012

Write a shell script to find the sum of the first n numbers.



echo "Enter a number: "
read num

i=1
sum=0

while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of first $num numbers is: $sum"
--------------------------------------------------------------------------------------------------------------
Enter a number:
5
The sum of first 5 numbers is: 15

0 comments:

Post a Comment