Q. Write a program that prints the sum of the even-indexed elements of L, minus the sum of the odd-indexed elements of L.


For example:

For list[1, 2, 3, 4, 5, 6] it should print -3 (that is, (1 + 3 + 5) - (2 + 4 + 6))

For list([1, 2, 3]) it should print 2 (that is, 1 - 2 + 3)


Answer =

 

L = eval (input("Enter the number list :- "))
even = odd = 0
for i in range (len(L)) :
    if i % 2 == 0 :
        even += L[ i ]
    else :
        odd += L[ i ]
print ("(sum of even-indexed elements) - (sum of odd-indexed elements) :-", even - odd)

 

2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. Replies
    1. It will add element of List a into b. It can also be written as :-
      b = b + a[i]

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post