144,016 views
5 votes
5 votes
Write a Python code to accept integer numbers in a list. Find and display the

sum and product of the numbers available at even and odd indices of the list,
respectively.

User Hitzg
by
3.2k points

1 Answer

4 votes
4 votes

#initializing list

list1 = []

# accepting values from user

n = int(input("Enter number of elements in list: "))

# iterating till the range

for i in range(0, n):

ele = int(input())

list1.append(ele)

# sum and product at even and odd indexes

even_sum = 0

odd_sum = 0

even_prod = 1

odd_prod = 1

# iterating each element in list

for j in range(len(list1)):

if j % 2 == 0:

even_sum += list1[j]

even_prod *= list1[j]

else:

odd_sum += list1[j]

odd_prod *= list1[j]

# Displaying sum and product of even and odd indices

print("Sum of elements at even indices:", even_sum)

print("Product of elements at even indices:", even_prod)

print("Sum of elements at odd indices:", odd_sum)

print("Product of elements at odd indices:", odd_prod)

User Sandesh Gupta
by
3.5k points