Sum of numbers in a given range in Python

sum of numbers in a given range in python

Sum of numbers in a given range

In this article we will see a python program to add numbers present between a given range. User need to enter upper limit and lower limit. 

For better understanding let’s suppose two values 3 and 8,

Then, Sum of all natural numbers between 3 and 8 will be (3+4+5+6+7+8) = 33.

So we will create a python program to calculate the sum of all natural numbers in a given range.

Algorithm

Step 1 : Ask the user to enter two numbers which will denote the starting and ending of the range for adding all natural numbers between that range. Step 2 : Declare a variable to store the addition and initialize it with 0. Step 3 : Use a loop to perform iterations for adding all natural numbers from starting range to the ending range. Step 4 : Use a statement for addition purpose. Step 5 : Print the result.

Python code

#Get the lower limit from user
lower_limit = int(input("Enter Lower limit: "))

#get upper limit from user
Upper_limit = int(input("Enter Upper limit: "))

#initialize submission as zero
sum=0

#iterate from lower to upper limit and add the numbers to sum.
for i in range(lower_limit,Upper_limit+1):
    sum=sum+i

#print sum value
print("Addition of numbers = ",sum)

Output

Enter Lower limit: 2
Enter Upper limit: 4
Addition of numbers =  9