CBSE Class 12 Computer Science Question 51 of 56

Functions — Question 51

Back to all questions
51
Question

Question 44

Write a method EvenSum(NUMBERS) to add those values in the tuple of NUMBERS which are even.

Solution
def EvenSum(NUMBERS):
    even_sum = 0
    for number in NUMBERS:
        if number % 2 == 0:
            even_sum += number
    return even_sum


n = eval(input("Enter the tuple: "))
result = EvenSum(n)
print("The sum of even numbers in the tuple is:", result)
Output
Enter the tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The sum of even numbers in the tuple is: 30
Answer

def
EvenSum
(
NUMBERS
):
even_sum
=
0
for
number
in
NUMBERS
:
if
number
%
2
==
0
:
even_sum
+=
number
return
even_sum
n
=
eval
(
input
(
"Enter the tuple: "
))
result
=
EvenSum
(
n
)
print
(
"The sum of even numbers in the tuple is:"
,
result
)
Output
Enter the tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
The sum of even numbers in the tuple is: 30