Thursday, May 1, 2008

Write a shell script to find the sum of even numbers upto ‘n’

echo “Enter upper limit”

read n

$i=2

while [$i -lt $n]

do

expr ‘$sum=$sum+$i’

expr ‘$i=$i+2’

done

echo “Sum is : $sum”

3 comments:

Anirudh M said...

Found many errors in your program. This works fine:


#! /bin/bash
echo enter limit
read n
i=2
while [ $i -lt $n ]
do
sum=$((sum+i))
i=$((i+2))
done
echo $sum.

Thanks.

gjshivraj said...

thank you so much!!

Unknown said...

thanks.....