Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Program to find the sum of numbers in a given range

Find Sum of numbers in a given range

In this python program, we will find the sum of numbers in a give range from first to second. In the Python program we can use for loop. In that for loop we have to start from the first range to last range which numbers should be entered by the user.
Let us suppose user enters two numbers are 5 and 15.
Then 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 = 110
Both numbers will included in the arithmetic additions.

Working:

Step 1. Start

Step 2. Initialize the sum variable to 0.

Step 3. Take a two inputs from the user for two Variables first and Second.

Step 4. Start a for loop from range starts from first value to Second +1 Value.

Step 5. In for loop for every cycle sum will be incremented by i.

Step 6.When condition gets false print sum.

Step 7. Stop 

Python Program:

# Program to find the sum of numbers in a given range in Python

first = int(input("Enter first number:"))
second = int(input("Enter second number:"))
sum = 0
for i in range(first, second+1):
    sum = sum + i

print("Sum of natural numbers in a given range:",sum)

# This code is contributed by Shubhanshu Arya (Prepinsta Placement Cell Student) 

Output:

Enter first number:5
Enter second number:15
Sum of natural numbers in a given range: 110