Sum of all even numbers in range L and R Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given two integers L and R, the task is to find the sum of all even numbers in range L and R. Examples: 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. Method-2: Find the sum all the natural numbers from L to R and subtract the sum of odd natural numbers in range L to R from it.Method-3: 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: C++ // C++ program to print the sum // of all even numbers in range L and R #include <bits/stdc++.h> using namespace std; // Function to return the sum of // all natural numbers int sumNatural(int n) { int sum = (n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R int sumEven(int l, int r) { return sumNatural(r/2) - sumNatural((l-1) / 2); } // Driver Code int main() { int l = 2, r = 5; cout << "Sum of Natural numbers from L to R is " << sumEven(l, r); return 0; } Java // Java program to print the sum // of all even numbers in range L and R import java.io.*; class GFG { // Function to return the sum of // all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R static int sumEven(int l, int r) { return sumNatural(r/2) - sumNatural((l-1) / 2); } // Driver Code public static void main (String[] args) { int l = 2, r = 5; System.out.println ("Sum of Natural numbers from L to R is "+ sumEven(l, r)); } } Python3 # Python 3 program to print the sum # of all even numbers in range L and R # Function to return the sum # of all natural numbers def sumNatural(n): sum = (n * (n + 1)) return int(sum) # Function to return sum # of even numbers in range L and R def sumEven(l, r): return (sumNatural(int(r / 2)) - sumNatural(int((l - 1) / 2))) # Driver Code l, r = 2, 5 print("Sum of Natural numbers", "from L to R is", sumEven(l, r)) # This code is contributed # by 29AjayKumar C# // C# program to print the sum // of all even numbers in range L and R using System; public class GFG{ // Function to return the sum of // all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R static int sumEven(int l, int r) { return sumNatural(r/2) - sumNatural((l-1) / 2); } // Driver Code static public void Main (){ int l = 2, r = 5; Console.WriteLine("Sum of Natural numbers from L to R is "+ sumEven(l, r)); } } PHP <?php // PHP program to print the sum of // all even numbers in range L and R // Function to return the sum of // all natural numbers function sumNatural($n) { $sum = ($n * ($n + 1)); return $sum; } // Function to return sum of // even numbers in range L and R function sumEven($l, $r) { return sumNatural((int)($r / 2)) - sumNatural((int)(($l - 1) / 2)); } // Driver Code $l = 2; $r = 5; echo "Sum of Natural numbers " . "from L to R is " . sumEven($l, $r); // This code is contributed // by Akanksha Rai ?> JavaScript <script> // Javascript program to print the sum // of all even numbers in range L and R // Function to return the sum of // all natural numbers function sumNatural(n) { let sum = Math.floor(n * (n + 1)); return sum; } // Function to return sum // of even numbers in range L and R function sumEven(l, r) { return sumNatural(Math.floor(r/2)) - sumNatural(Math.floor(l-1) / 2); } // driver program let l = 2, r = 5; document.write ("Sum of Natural numbers from L to R is "+ sumEven(l, r)); </script> Output: Sum of Natural numbers from L to R is 6 Time Complexity: O(1), since there is no loop or recursion.Auxiliary Space: O(1), since no extra space has been taken. Comment More infoAdvertise with us Next Article Sum of all natural numbers in range L to R S swetankmodi Follow Improve Article Tags : Mathematical DSA math number-theory Numbers +1 More Practice Tags : Mathematicalnumber-theoryNumbers Similar Reads Sum of all odd natural numbers in range L and R Given two integers L and R, the task is to find the sum of all odd natural numbers in range L and R inclusive. Examples: Input: L = 2, R = 5 Output: 8 3 + 5 = 8 Input: L = 7, R = 13 Output: 40 A naive approach is to traverse from L to R and summate the elements to get the answer. An efficient approa 4 min read Sum of all even factors of numbers in the range [l, r] Given a range [l, r], the task is to find the sum of all the even factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 22 factors(6) = 1, 2, 3, 6, evenfactors(6) = 2, 6 sumEvenFactors(6) = 2 + 6 = 8 factors(7) = 1, 7, No even factors factors(8) = 1, 2, 4, 8, evenfactors( 15+ min read Sum of all natural numbers in range L to R Given a range L and R, the task is to find the sum of all natural numbers in range L to R. Examples: Input: L = 2, R = 5 Output: 14 2 + 3 + 4 + 5 = 14 Input: L = 10, R = 20 Output: 165 A naive approach is to traverse from L to R and add all the elements one by one to get the sum.An efficient approac 4 min read Sum of all odd factors of numbers in the range [l, r] Given a range [l, r], the task is to find the sum of all the odd factors of the numbers from the given range.Examples: Input: l = 6, r = 8 Output: 32 factors(6) = 1, 2, 3, 6, oddfactors(6) = 1, 3 sum_Odd_Factors(6) = 1 + 3 = 4 factors(7) = 1, 7, oddfactors(6) = 1 7, sum_Odd_Factors(7) = 1 + 7 = 8 fa 6 min read Calculate the sum of sum of numbers in range L to R Given two numbers L and R. The task is to find the sum of numbers in the range L to R. Examples: Input: L = 3, R = 6Output: 40Explanation: 3 + 3+4 + 3+4+5 + 3+4+5+6 = 40 Input: L = 5, R = 6Output: 16 Approach: This problem is formula-based. For the illustration given below, observe the number of tim 4 min read Sum of all numbers divisible by 6 in a given range Given a range L-R, find the sum of all numbers divisible by 6 in range L-RL and R are very large.Examples: Input : 1 20 Output : 36 Explanation: 6 + 12 + 18 = 36 Input : 5 7 Output : 6 Explanation: 6 is the only divisible number in range 5-7 A naive approach is be to run a loop from L to R and sum u 6 min read Like