Write a python code that fulfils the following requierments.

QUESTION

Write a python code that fulfills the following requirements

1- read two positive numbers

2-perform this computation :L smaller*number, and Kbigger*number

3-find the sum from L to K and return the sum*

for example, if L=4, and K=8, the result should be 4+6+7+8, and 25 is returned

*you can use any method to find the smaller/bigger number

**you can use any method/way to sum/add

 

SUMMARY

Two integers are taken in as the input. A small number out of them is assigned to ‘s’ and the larger number is assigned to ‘l’. The sum of all integers from l to k is then found and returned as output to the console.

 

EXPLANATION

Initially, a and b(int) are taken in as input from the user. The method small() is used to assign the smaller number of a and b to a variable ‘s’ and the larger number of a and b to a variable ‘l’. This method is then called. Another method called sumSL() is defined. The variables s and l are passed to this method. A loop is made to iterate from s to l. This loop is used to find the sum of all integers from s to l. The sum is returned and printed as output to the console. Code is written in python.

 

CODE (python code that fulfills the following requirements)

# getting two integers as input from user 
a=int(input())
b=int(input())
 
# method to assign smaller number of two 
# small number to 's' and larger number to 'l'
def small(a,b,s,l):
    if a<b:
        s=a 
        l=b
    else:
        s=b 
        l=a
 
s=a 
l=b 
# calling small method 
small(a,b,s,l)
 
# to find the sum of integers from s to l 
def sumSL(s,l):
    sum=0
    for i in range(s,l+1):
        sum=sum+i
    return sum 
 
# printing the sum of integers 
print(sumSL(s,l))

 

OUTPUT

 

Also Read: Design a Python program by utilizing from lists to define the number of generators n

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *