Given two integers L and R, the task is to find the sum of all even numbers in range L and R.
Input: L = 2, R = 5 Output: 6 2 + 4 = 6 Input: L = 3, R = 8 Output: 18
Method-1: Iterate from L to R and sum all the even numbers in that range.
- Find the sum of all even numbers up to R i.e. No. of even numbers up to R will be R/2.
- Find the sum of all even numbers up to L-1 i.e. No. of even numbers up to L-1 will be (L-1)/2.
- Then subtract sumUptoL from sumuptoR.
Sum of all even numbers up to any N will be:
R*(R+1) where R = N/2
Below is the implementation of the above approach:
Sum of Natural numbers from L to R is 6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Numbers within a range that can be expressed as power of two numbers
- Sum of all the prime numbers in a given range
- Find XOR of numbers from the range [L, R]
- Count Odd and Even numbers in a range from L to R
- Prime numbers in a given range using STL | Set 2
- Sum of Fibonacci Numbers in a range
- Panarithmic numbers within a given range
- Sum of all numbers in the given range which are divisible by M
- Sum of all numbers divisible by 6 in a given range
- Check whether XOR of all numbers in a given range is even or odd
- Count of numbers having only 1 set bit in the range [0, n]
- Sum of all odd natural numbers in range L and R
- Sum of all odd factors of numbers in the range [l, r]
- Sum of all even factors of numbers in the range [l, r]
- Sum of all natural numbers in range L to R
- Count numbers in range 1 to N which are divisible by X but not by Y
- Numbers that are not divisible by any number in the range [2, 10]
- Count factorial numbers in a given range
- Sum of all Non-Fibonacci numbers in a range for Q queries
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.