Python program to print sum of N natural numbers

sum of n natural number

Sum of N natural numbers using Python :

In this article we will perform addition on first n natural numbers, where n is a number up to which we have to perform addition. So we create a python program to print the sum of n natural numbers.

For better understanding let’s suppose the value of n is 5 then,

Sum of first 5 natural numbers will be (1+2+3+4+5) = 15.

So we create a python program to calculate sum of first n natural numbers.

Algorithm

Step 1 : Ask the user to enter a  number which will denote the limit.

Step 2 : Declare a variable to store the addition and initialize it with 0.

Step 3 : Use a loop to perform iterations for adding natural numbers from 1 till the limit entered by the user.

Step 4 : Use a statement for addition purpose.

Step 5 : Print the result.

 

Python Code

n=int(input("Enter a number: "))
sum=0
for i in range(n+1):
sum=sum+i 
print("Sum is:",sum)

Output

Enter a number: 2
Sum is: 3