Sum of EvenSend FeedbackProblem Statement:Write a program that takes a tuple as input and prints the sum of all the even numbers in the tuple.Input:A tuple containing integer elements.Output:The sum of all the even numbers in the tuple.Sample Input:(2, 3, 4, 6, 7)Sample Output:12
Question
Sum of EvenSend FeedbackProblem Statement:Write a program that takes a tuple as input and prints the sum of all the even numbers in the tuple.Input:A tuple containing integer elements.Output:The sum of all the even numbers in the tuple.Sample Input:(2, 3, 4, 6, 7)Sample Output:12
Solution 1
Here is a step-by-step guide to solve the problem in Python:
- First, define a function that takes a tuple as an argument. Let's call this function
sum_of_even.
def sum_of_even(numbers):
- Inside this function, initialize a variable
sumto 0. This variable will be used to keep track of the sum of the even numbers.
sum = 0
- Next, iterate over each number in the tuple using a for loop.
for num in numbers:
- Inside the loop, use an if statement to check if the current number is even. You can do this by using the modulo operator
%. If a number modulo 2 equals 0, then the number is even.
if num % 2 == 0:
- If the number is even, add it to the
sum.
sum += num
- After the loop, return the
sum.
return sum
- Now you can call this function with a tuple of numbers to get the sum of the even numbers.
print(sum_of_even((2, 3, 4, 6, 7))) # Output: 12
Here is the complete code:
def sum_of_even(numbers):
sum = 0
for num in numbers:
if num % 2 == 0:
sum += num
return sum
print(sum_of_even((2, 3, 4, 6, 7))) # Output: 12
Solution 2
Here is a step-by-step guide to solve the problem in Python:
Step 1: Define the tuple. For example, we can use the tuple from the sample input:
numbers = (2, 3, 4, 6, 7)
Step 2: Initialize a variable to store the sum of the even numbers. We can start with a sum of 0:
even_sum = 0
Step 3: Iterate over each number in the tuple. For each number, check if it is even. If it is, add it to the sum:
for num in numbers:
if num % 2 == 0:
even_sum += num
Step 4: Print the sum of the even numbers:
print(even_sum)
When you put it all together, the complete program looks like this:
numbers = (2, 3, 4, 6, 7)
even_sum = 0
for num in numbers:
if num % 2 == 0:
even_sum += num
print(even_sum)
When you run this program, it will print 12, which is the sum of the even numbers in the tuple.
Similar Questions
Write a program to accept an integer N and print the sum of first N odd numbersInput Format:An integer range is given inputOutput Format:Print only calculated sum.Constraints:1 <= N <= 10^16 Sample Input 1:2Sample Output 1:4Sample Input 2:10Sample Output 2:100
Problem Statement:Find the sum of even numbers in the given range.Input Format:Accept two integer as inputOutput Format:Print the output as sum of even number available in the given rangeConstraints:1 <= INPUT <= 10^15Sample Input 1:10 20Sample Output 1:90Sample Input 2:30 33Sample Output 2:62
Define a function even_sum(num), where num is a positive number. The function iterates all the integers numbers from 0 to num and sum them up if they are even numbers.Exampleeven_sum(4) -> 2+4 = 6even_sum(8.5) -> 2+4+6+8 = 20
What will be the output of the following program ?tuple = {} tuple[(1,2,4)] = 8 tuple[(4,2,1)] = 10 tuple[(1,2)] = 12 _sum = 0 for k in tuple: _sum += tuple[k] print(len(tuple) + _sum)
Write a C++ program to find the sum of even and odd n natural numbers
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.