Ex. No.: 2(b)(i)
Sum of n numbers
To write a shell script to find the sum of n numbers.
Step 1: Start the program
Step 2: Enter the value of n
Step 3: Declare the variables of i and sum and both are initialized to zero.
Step 4: Perform the addition of n numbers using while loop
Step 5: Print the sum.
Step 6 : Stop the program
echo "Enter the value of n "
i = 0
sum = 0
while[$i –lt $n]
sum=’expr $sum + expr $i’
i = ‘expr $i + 1’
echo "Sum of n natural numbers are : $sum”
[csestaff@localhost csestaff]$ sh sum.sh
Sum of n natural numbers
Enter the value of n : 3
Sum of n natural numbers are : 6
Thus, the shell script to find the sum of n natural numbers is entered and its output was verified.