Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Python Program to find the sum of numbers in a given range
Find Sum of numbers in a given range
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.
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
# 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)
Enter first number:5
Enter second number:15
Sum of natural numbers in a given range: 110