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
Question
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
Solution
Sure, here is a Python function that does exactly what you described:
def even_sum(num):
# Initialize the sum to 0
sum = 0
# Iterate over all numbers from 0 to num
for i in range(int(num) + 1):
# If the number is even, add it to the sum
if i % 2 == 0:
sum += i
# Return the sum
return sum
This function works by iterating over all numbers from 0 to num (inclusive). If a number is even (i.e., it is divisible by 2), it is added to the sum. The sum is then returned as the result of the function. Note that if num is a float, it is converted to an integer before the range is created. This is because range only works with integers in Python.
Similar Questions
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
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 cumsum(num). The function returns the sum of all positive integers smaller or equal to num.Examplecumsum(4) -> 1+2+3+4 = 10cumsum(5.5) -> 1+2+3+4+5 = 15Hintint() converts a float type value into an interger.
Raj is organizing a fundraising event and wants to distribute prizes to volunteers based on their contributions. He has decided to reward volunteers based on the sum of their contributions, considering only even-numbered contributions. Raj is seeking assistance in quickly calculating the sum of even numbers within a given range using a function called sumOfEven(). Can you help him with this task?Note: This question helps in clearing technical coding tests for companies like Amazon and Toluna.Input format :The input consists of two integers separated by a space, representing the lower limit L and the upper limit R.Output format :The output displays a single integer, representing the sum of even numbers from L to R (both inclusive).Refer to the sample output for formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ L < R ≤ 100
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.