Answer to Question #185502 in Python for Woyengimiesindo Malati

Question #185502

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


1
Expert's answer
2021-04-28T16:49:44-0400
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS