How to write a program to print the sum of first n natural numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to write a program to print the sum of first n natural numbers

python

9th Sep 2018, 9:08 AM
Abhinaya
Abhinaya - avatar
4 Answers
+ 6
have a sum variable, iterate from 1 to n, and add each number to the sum variable
9th Sep 2018, 9:16 AM
hinanawi
hinanawi - avatar
+ 4
or use the formula n*(n+1)//2
9th Sep 2018, 9:18 AM
michal
+ 2
a better approach would be to directly use the formula - n*(n + 1) / 2
9th Sep 2018, 9:18 AM
Nikunj Arora
Nikunj Arora - avatar
+ 1
ok thanks
9th Sep 2018, 2:53 PM
Abhinaya
Abhinaya - avatar