Posted by: comingsoon February 6, 2013
Login in to Rate this Post:
0
?
Thanks kiddo, I'm now almost in the solutin. there is little thing to correct.
Code:
N = int(input("N: "))
i=1
Sum1=0
Sum2=0
while i <= N:
if i%2==0: #If i is even
print("-",i, end=' ')
Sum1=Sum1+i
else: #if i is odd
print("+", i, end=' ')
Sum2=Sum2+i
i=i+1
print("=", Sum2-Sum1)
Result:
N: 5
+ 1 - 2 + 3 - 4 + 5 = 3
>>> ================================ RESTART ================================
>>>
N: 3
+ 1 - 2 + 3 = 2
>>> ================================ RESTART ================================
>>>
N: 4
+ 1 - 2 + 3 - 4 = -2
How to remove + sign before 1
should look like this
1-2+3-4=-2
Code:
N = int(input("N: "))
i=1
Sum1=0
Sum2=0
while i <= N:
if i%2==0: #If i is even
print("-",i, end=' ')
Sum1=Sum1+i
else: #if i is odd
print("+", i, end=' ')
Sum2=Sum2+i
i=i+1
print("=", Sum2-Sum1)
Result:
N: 5
+ 1 - 2 + 3 - 4 + 5 = 3
>>> ================================ RESTART ================================
>>>
N: 3
+ 1 - 2 + 3 = 2
>>> ================================ RESTART ================================
>>>
N: 4
+ 1 - 2 + 3 - 4 = -2
How to remove + sign before 1
should look like this
1-2+3-4=-2