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
Question
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
Solution
Sure, I can help with that. Here is a Python function that calculates the sum of even numbers within a given range:
def sumOfEven(L, R):
sum = 0
for i in range(L, R+1):
if i % 2 == 0:
sum += i
return sum
This function works by iterating over each number in the range from L to R (inclusive). If the current number is even (i.e., it leaves a remainder of 0 when divided by 2), it is added to the sum. Once all numbers have been processed, the function returns the sum.
You can call this function with the lower and upper limits as arguments, like this:
print(sumOfEven(1, 10))
This will print the sum of all even numbers from 1 to 10.
Similar Questions
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
Nimisha is assigned to calculate the sum of divisors for a given positive integer. She needs a function called sumOfDivisors to calculate the sum of divisors for a given positive integer.Help her with the program.Example: If the number is 12, the sum of its divisors would be 1+2+3+4+6+12 = 28.Input format :The input consists of an integer n.Output format :The output prints the sum of all the divisors of n (inclusive).
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
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
(This is from Programming Exercise P4.1 at the end of Chapter 4 of Java for Everyone.) Write program fragments (i.e., you do not need to write complete programs) with loops that compute:The sum of all even numbers between 2 and 100 (inclusive).The sum of all squares between 1 and 100 (inclusive).All powers of 2 from 20 up to 220 (inclusive).The sum of all odd numbers between a and b (inclusive), where a and b are integer variables with a ≤ b.The sum of all digits at odd positions (right-to-left starting at 1 as the right-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 7 + 6 + 3 = 16.)The sum of all digits at odd positions (left-to-right starting at 1 as the left-most digit) of a numeric input. (For example, if the input is 432677, the sum would be 4 + 2 + 7 = 13.)
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.