Posted by: Kiddo February 6, 2013
Login in to Rate this Post:
0
?
While changing the syntax (i%2 instead of i/2) is understandable, I don't know why you changed the logic? If you keep my logic intact, you should get it the way you want it, i.e 1-2+3-4+5=3
N = int(input("N: "))
i=1
Sum=0
while i <= N:
if i%2==0: #If i is even
print(i, "+", end=' ')
Sum=Sum-i
else: #if i is odd
print(i, "-", end=' ')
Sum=Sum+i
i=i+1
print("=", Sum)
N = int(input("N: "))
i=1
Sum=0
while i <= N:
if i%2==0: #If i is even
print(i, "+", end=' ')
Sum=Sum-i
else: #if i is odd
print(i, "-", end=' ')
Sum=Sum+i
i=i+1
print("=", Sum)