Find the sum of even numbers

Problem Statement

Mr. Ram is given a problem to find the sum of all even numbers in the given list of numbers.

Write a program to read the numbers, and then print the sum of all the even numbers in the given input.

Input

The first line of input contains space separated integer values.

Output

Print the sum of all even numbers present in the given input.

As is

The boilerplate code is already there to read the inputs from user into a list, in the main() function. Also it calls the sumOfEven() function, and prints the returned value. Do not edit this code.

To do ✍

sumOfEven() has one parameter: nums. Write code inside the function body to find the sum of all the even numbers in the list nums. And return the sum.

Example 1

Test input

2 1 3 12 10 7 8 5 

Test output

Pangram

Explanation

The even numbers in given list:
2 12 10 8

Sum = 2 + 12 + 10 + 8
    = 32

Useful References

Python Program

# Complete the following function
# Return the sum of even numbers in the nums list
def sumOfEven(nums):
    sum = 0



    return sum

# Boilerplate code - Do not edit the following
def main():
    nums = [int(x) for x in input().split()]
    print(sumOfEven(nums))

if __name__ == "__main__":
    main()

Testcase 1

2 1 3 12 10 7 8 5 
32

Testcase 2

5 1 0 2 3 6 7 1 4 0 5 0 2
14

Testcase 3

0 1 3 5 7
0

Testcase 4

15 60 90 41 23 58
208
# Complete the following function
# Return the sum of even numbers in nums list
def sumOfEven(nums):
    sum = 0
    for num in nums:
        if num % 2 == 0:
            sum += num
    return sum

# Boilerplate code - Do not edit the following
def main():
    nums = [int(x) for x in input().split()]
    print(sumOfEven(nums))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍