Sunday, 20 June 2010

Algorithm to find the sum of first n natural numbers

Algorithm to find the sum of first n natural numbers:
1. Read n.
2. Initialize i=1.
3. Initialize sum s=0.
4. Repeat step 4 to step 6 until i=n
5. Calculate s=s+i.
6. Calculate i=i+1.
7. print sum
8. Stop.


No comments:

Post a Comment