Algorithm
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize 0 to sum and 1 to i
Step 4 : Add i to sum
Step 5 : Increment i by 1
Step 6: Repeat steps 4–6 until i n
Step 7 : Print sum
Step 8 : Stop
Program (sum1ton.sh)
# Sum of 1+2+3+ ... +N numbers
echo -n "Enter N value : "
read n
sum=0
i=1
until [ $i -gt $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of n numbers is $sum"
Output
[vijai@localhost loops]$ sh sum1ton.sh
Enter N value : 26
The sum of n numbers is 351
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize 0 to sum and 1 to i
Step 4 : Add i to sum
Step 5 : Increment i by 1
Step 6: Repeat steps 4–6 until i n
Step 7 : Print sum
Step 8 : Stop
Program (sum1ton.sh)
# Sum of 1+2+3+ ... +N numbers
echo -n "Enter N value : "
read n
sum=0
i=1
until [ $i -gt $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of n numbers is $sum"
Output
[vijai@localhost loops]$ sh sum1ton.sh
Enter N value : 26
The sum of n numbers is 351
No comments:
Post a Comment