+ 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)
7 Antworten
+ 5
What about
print(-1*2000/2)
+ 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)
+ 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)
+ 2
sum=0
for i in range(1,2001):
sum=sum+i*((-1)**(i+1))
print(sum)
+ 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
+ 1
Thank you all. I got it now🙏🏾
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.