Given a space-separted list of integers as input, write a program to add the elements in the given range and print their sum
You will be given M multiple range queries,where
You should print the sum of numbers that belong to the corresponding range
note: while checking if the number belongs to a range,including the string and numbers of range as well
Input
The first line of input is space-separated integers
The secondline of input is a positive integers M denoting the number of queries
The next M line contain two space-separated integer
Output:
The output should be M line printing the sum of each includes range
sample input 1:
1 2 2 3 3 3 4 5 6
2
sampleInput2:
6 6 14 20 8 -2 2 -3
4
1 2
2 5
3 6
0 4
sampleOutput:
2
2
12
2
#input variable for entering numbers
numbers = str(input("Enter your numbers separated by space: ")).split(" ")
print(numbers)
Prod = 1
s = ""
#loop for calculations on our input list
for i in range(0, len(numbers)):
Prod = Prod * int(numbers[i])
s = s + numbers[i] + " x "
print("Product of list (",s,") = ",Prod)
#this code considered by expert Suhrob Umarov
#second task
#input variable for adding range
number = input("Enter Number to calculate sum ")
number = int (number)
sum = 0
#loop for calculation
for num in range(0, number+1, 1):
sum = sum+num
#to print calculation
print("SUM of first", number, "numbers is:", sum )
#This code considered by Suhrob Umarov
Comments
Leave a comment