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 = 5Output:6 2 + 4 = 6Input: L = 3, R = 8Output: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; ` `} ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

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.

## Recommended Posts:

- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Sum of all even factors of numbers in the range [l, r]
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Check whether XOR of all numbers in a given range is even or odd
- Sum of range in a series of first odd then even natural numbers
- Count of permutations such that sum of K numbers from given range is even
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Count Odd and Even numbers in a range from L to R
- Average of even numbers till a given even number
- Rearrange array such that all even-indexed elements in the Array is even
- Sum and Product of all even digit sum Nodes of a Singly Linked List
- Sum of all odd natural numbers in range L and R
- Count ways to express even number ānā as sum of even integers
- Generate an array of given size with equal count and sum of odd and even numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.