In this post, we will see how to find sum of natural numbers.
- Initialize sum to 0
- Iterate over range(n+1) and add to sum
- You will get sum of all natural number in the end
Let’s see very simple python program for it.
n=11 sum=0 for i in range(n+1): sum+=i print("Sum is",sum)
Can you find simpler solution?
Well, you can simply find sum of all natural numbers using below formula
n=11 sum=0 sum =(n*(n+1))/2 print("Sum is",sum)
That’s all about finding sum of natural numbers.