Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
How to Find the Sum of Natural Numbers using Python?
You can use while loop to successively increment value of a variable i by one and adding it cumulatively.
s,i=0,0
n=10
while i<n:
i=i+1
s=s+i
print ("sum of first 10 natural numbers",s)
For loop is also used to loop over a range of natural numbers and add them cumulatively.
s=0
for i in range(11):
s=s+i
print ("sum of first 10 natural numbers",s)
Lastly using the built in function sum() also gives sum of a range of numbers
s=sum(range(11))
print ("sum of first 10 natural numbers",s)
Advertisements
