We will discuss here how to find the sum of first n natural numbers.
Sum=S = 1 + 2 + 3 + 4 + 5 + ……………….. + n
❖ Algorithm
Step 1 : Start
Step 2 : Display “Enter the number of N”
Step 3 : read n
Step 4 : sum ← 0
Step 5 : Repeat Step 6
i ← 0
While i ≤ n
Step 6 : sum ← sum + i
Step 7 : Display “the sum is” sum
Step 8 : Stop
❖ Source Code
#! /bin/bash # Author : Sounak Pal #website : cs.sounak.in ## Script to add first n numbers echo "Enter the number of N" read n sum=0 for (( i=0; i<=n; i++ )) do sum=$((sum + I)) done echo -e "The sum of first N number is \t $sum"
❖ Input:
Enter the number of N
6
❖ Output:
The sum of first N number is 21