Write a program that prints the sum of the even-indexed elements of L, and
minus the sum of the odd-indexed elements of L.
For example:
For list L=[1,2,3,4,5,6] it should print -3, that is (1+3+5)-(2+4+5)
For list L=[1,2,3] it should print 2, that is (1+3)-(2)
Answers
The following cσdes have been written using Python.
n = int(input("Enter the number of elements you'd like to enter: "))
l = list()
print()
for i in range(n):
x = int(input("Enter the number: "))
l.append(x)
print()
print()
print(l, "is your given list.")
print()
ll = len(l)
pl = list()
nl = list()
for i in range(ll):
if i%2 == 0:
pl.append(l[i])
else:
nl.append(l[i])
print()
s = sum(pl) - sum(nl)
print(s, "is the desired output.")
"""here elements are already given
note: remember that index in lists are given in the form of 0,1,2,3,4,5,6...."""
L=[1,2,3,4,5,6]
b=[ ]
c=[ ]
for i in range(0,len(L),2):
b.append(L[i])
c.append(L[i-1])
e=sum(b)
o=sum(c)
print("list of elements at even indexes are ",b,"\n their sum is",e)
print("list of elements at odd indexes are ",c,"\n their sum is",o)
print("hence sum of even index minus sum of odd index is ",e-o)
or
"""here we will take list as an input from the user """
L=eval(input("enter elements of the list :"))
b=[ ]
c=[ ]
for i in range(0,len(L),2):
b.append(L[i])
c.append(L[i-1])
e=sum(b)
o=sum(c)
print("list of elements at even indexes are ",b,"\n their sum is",e)
print("list of elements at odd indexes are ",c,"\n their sum is",o)
print("hence sum of even index minus sum of odd index is ",e-o)
----------------------------------------------------------------------------------------------------------
please refer the images that i have attached
am i the only lass who knows CS