Monday, October 28, 2013

Shell script to find the sum of the first n numbers.

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


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

No comments:

Post a Comment