In: Computer Science
In Python:
Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second.
To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at least 1 decision structure.
Examples:
sum_odd(0, 5) should return the value 9 as (1 + 3 + 5) = 9
sum_odd(6, 10) should return the value 16
sum_odd(13, 20) should return the value 64
sum_odd(7, 11) should return the value 27
/* PLEASE INDENT CODE FROM SCREENSHOT TO RUN IT WITHOUT ERRORS */
def sum_odd(x,y):
sum = 0
for i in range(x,y+1): # runs from x to y(inclusive)
if(i%2): # check if i not divide by 2
sum = sum + i
return sum
def main():
num1 = int(input("Enter 1st integer: ")) #take input
num2 = int(input("Enter 2nd integer: "))
print("Sum of Odd: {}".format(sum_odd(num1,num2)))
if __name__ == '__main__':main()
/* PLEASE UPVOTE */