+ 1

Write a python program to compute the sum 1 − 2 + 3 − 4 + ··· + 1999 − 2000.

Hi anyone, please. I need solution to the above question, I tried doing it this way, and want to confirm if I'm right or not: sum = 0 for NUM in range(1, 2001): series = NUM - (NUM + 1) + (NUM - 1) sum += series print(sum)

4th Jun 2022, 6:00 PM
agboola zainab
7 Réponses
+ 5
What about print(-1*2000/2)
4th Jun 2022, 6:54 PM
Jihad AL AKL
Jihad AL AKL - avatar
+ 4
I kept your code as much as possible, only I added step 2 in the range, and made your code less complicated. I think you were on the right track: sum = 0 for NUM in range(1, 2001, 2): sum += NUM - (NUM + 1) print(sum)
4th Jun 2022, 7:08 PM
Paul
Paul - avatar
+ 3
There are number of ways to achieve the task, if you notice all the even numbers are subtracted and odd are added in the series. You can use 'if statements' to achieve the desired result or if you wanna use mathematical formula, then see the Mihir Lalwani answer sum = 0 for i in range(1, 2001): if i % 2 == 0: sum -= i else: sum += i print(sum)
4th Jun 2022, 6:29 PM
Sandeep
Sandeep - avatar
+ 2
sum=0 for i in range(1,2001): sum=sum+i*((-1)**(i+1)) print(sum)
4th Jun 2022, 6:22 PM
Mihir Lalwani
Mihir Lalwani - avatar
+ 2
1-2 = -1 3-4 = -1 5-6 = -1 ... 2000-1999 = -1 So I think it should be (2000//2)* -1 for n = 10 1-2+3-4+5-6+7-8+9-10 (-1)+(-1)+(-1)+(-1)+(-1) = -5 which is (10//2) * -1
4th Jun 2022, 6:29 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Thank you all. I got it now🙏🏾
5th Jun 2022, 10:48 AM
agboola zainab
0
Hello again! Please I need help with this: Write a python program that asks the user to enter a word and then capitalizes every other letter of that word. So if the user enters rhinoceros, the program should print rHiNoCeRoS.
13th Jun 2022, 10:13 PM
agboola zainab