Sum of Even Elements

Calculate sum of even elements.

Logic BuildingIntermediate
Logic Building
# Take array
n = int(input("Enter array size: "))
arr = []
for i in range(n):
    arr.append(int(input(f"Element {i+1}: ")))

# Sum even elements
sum_even = 0
for element in arr:
    if element % 2 == 0:
        sum_even += element

print(f"Sum of even elements: {sum_even}")

Output

Enter array size: 5
Element 1: 2
Element 2: 3
Element 3: 4
Element 4: 5
Element 5: 6
Sum of even elements: 12

Filter even elements and sum.

Key Concepts:

  • Check if element is even
  • Add to sum if even
  • Conditional accumulation