Sum of Even Numbers
EasyPractice library · +10 XPbasicsloopsarithmetic
Given a list of integers, calculate the sum of all the even numbers in the list. Print the total sum.
Input
The first line contains a single integer N, the number of integers in the list. The second line contains N space-separated integers.
Output
Print a single integer, the sum of all even numbers in the list.
Constraints
1 <= N <= 1000 -1000 <= a_i <= 1000, where a_i is the i-th integer in the list.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 1 2 3 4 5
Output
6
The even numbers are 2 and 4. Their sum is 2 + 4 = 6.
Example 2
Input
4 -2 0 1 3
Output
-2
The even numbers are -2 and 0. Their sum is -2 + 0 = -2.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 1 2 3 4 5
Example 2 input
4 -2 0 1 3