Write a program with loops that compute the sum of all odd numbers between a and b
(inclusive), where a and b are inputs.
Sample Run
Enter the lesser integer: 1
Enter the greater integer: 10
The sum of all odd numbers from 1 to 10 = 25
a=int(input("Enter the first integer:"))
b=int(input("Enter the last integer"))
if(a%2==0 & b%2==20):
n=(((b-1)-(a+1))/2) + 1
sum=n*(a+b)/2
print(sum)
else:
if(a%2 == 0 & b%2 == 1):
n=(((b)-(a+1))/2)+1
sum= n*(a + b+1)/2
print(sum)
else:
if (a%2==1 & b%2==1):
n =(((b)-(a))/2)+1
sum=n*(a+b)/2
print(sum)
print(sum)
Comments
Leave a comment