Sequence Sum in Python

In this class, we write code for sequence sum in python.

For Complete YouTube Video: Click Here

Table of Contents

Sequence Sum

Q) Given input three integers. The integers are taken in three variables, i,j,k.

Condition: always i and k should be less than j.

sequence sum is given as the sum of all the values from i to j and then from j to k.

Example: i=5, j =10, k =6

output: 5+6+7+8+9+10+9+8+7+6 = 75

Logic

Logic is straightforward. Loop from i to j. take a sum variable.

Each time update the sum.

Again loop from j to k and update the sum. Here loop in reverse.

From the above example, j value is not considered when doing the sum from j to k.

Program

i=int(input("enter i value"))
j=int(input("enter j value"))
k=int(input("enter k value"))
sumval=0
for l in range(i,j+1):
    sumval+=l
for m in range(j-1,k-1,-1):
    sumval+=m
print(sumval)