Calculate the sum of 1 to n using loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Calculate the sum of 1 to n using loops

N = int(input()) a=0 i=1 while i<=N: a=a+i i=i+1 print (a)

27th Jan 2022, 6:15 PM
VANTARI MANI
2 Answers
+ 4
n = int(input()) print (sum(range(n+1)))
27th Jan 2022, 6:37 PM
Shadoff
Shadoff - avatar
+ 1
You can also use this formula: n * (n+1) // 2
27th Jan 2022, 7:20 PM
Denise Roßberg
Denise Roßberg - avatar