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
- Numbers within a range that can be expressed as power of two numbers
- Prime numbers in a given range using STL | Set 2
- Check whether XOR of all numbers in a given range is even or odd
- Sum of all natural numbers in range L to R
- Sum of all odd natural numbers in range L and R
- Count Odd and Even numbers in a range from L to R
- Sum of all numbers divisible by 6 in a given range
- Sum of all the prime numbers in a given range
- Count the numbers divisible by 'M' in a given range
- Print all Good numbers in given range
- Sum of greatest odd divisor of numbers in given range
- Count factorial numbers in a given range
- Find numbers with K odd divisors in a given range
- Sum of all odd length palindromic numbers within the range [L, R]
- Sum of range in a series of first odd then even natural numbers