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 = 5Output:14 2 + 3 + 4 + 5 = 14Input:L = 10, R = 20Output: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 approach** is to use the formula for the sum of first N natural numbers. The idea of the inclusion-exclusion principle helps to solve the above problem. Find the sum of natural numbers till **R** and **L-1** and then subtract *sum(R)-sum(l-1)*.

Below is the implementation of the above approach:

## C++

`// C++ program to print the sum ` `// of all 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)) / 2; ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the sum ` `// of all numbers in range L and R ` `int` `suminRange(` `int` `l, ` `int` `r) ` `{ ` ` ` `return` `sumNatural(r) - sumNatural(l - 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `l = 2, r = 5; ` ` ` `cout << ` `"Sum of Natural numbers from L to R is "` ` ` `<< suminRange(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to print the sum ` `// of all numbers in range L and R ` ` ` `class` `GFG{ ` `// Function to return the sum of ` `// all natural numbers ` `static` `int` `sumNatural(` `int` `n) ` `{ ` ` ` `int` `sum = (n * (n + ` `1` `)) / ` `2` `; ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the sum ` `// of all numbers in range L and R ` `static` `int` `suminRange(` `int` `l, ` `int` `r) ` `{ ` ` ` `return` `sumNatural(r) - sumNatural(l - ` `1` `); ` `} ` ` ` `// 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 "` `+suminRange(l, r)); ` ` ` `} ` `} ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to print the sum of ` `# all numbers in range L and R ` ` ` `# Function to return the sum of all natural numbers ` `def` `sumNatural(n): ` ` ` ` ` `sum` `=` `(n` `*` `(n` `+` `1` `))` `/` `/` `2` ` ` ` ` `return` `sum` ` ` `# Function to return the sum ` `# of all numbers in range L and R ` `def` `suminRange(l, r): ` ` ` `return` `sumNatural(r) ` `-` `sumNatural(l` `-` `1` `) ` ` ` `# Driver Code ` `l ` `=` `2` `; r` `=` `5` `print` `(` `"Sum of Natural numbers from L to R is "` `,suminRange(l, r)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to print the sum ` `// of all numbers in range L and R ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to return the sum ` `// of all natural numbers ` `static` `int` `sumNatural(` `int` `n) ` `{ ` ` ` `int` `sum = (n * (n + 1)) / 2; ` ` ` `return` `sum; ` `} ` ` ` `// Function to return the sum ` `// of all numbers in range L and R ` `static` `int` `suminRange(` `int` `l, ` `int` `r) ` `{ ` ` ` `return` `sumNatural(r) - ` ` ` `sumNatural(l - 1); ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `l = 2, r = 5; ` ` ` `Console.WriteLine(` `"Sum of Natural numbers "` `+ ` ` ` `"from L to R is "` `+ ` ` ` `suminRange(l, r)); ` `} ` `} ` ` ` `// This code is contributed by akt_mit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to print the sum ` `// of all numbers in range L and R ` ` ` `// Function to return the sum of ` `// all natural numbers ` `function` `sumNatural(` `$n` `) ` `{ ` ` ` `$sum` `= (` `$n` `* (` `$n` `+ 1)) / 2; ` ` ` `return` `$sum` `; ` `} ` ` ` `// Function to return the sum ` `// of all numbers in range L and R ` `function` `suminRange(` `$l` `, ` `$r` `) ` `{ ` ` ` `return` `sumNatural(` `$r` `) - ` ` ` `sumNatural(` `$l` `- 1); ` `} ` ` ` `// Driver Code ` `$l` `= 2; ` `$r` `= 5; ` `echo` `"Sum of Natural numbers "` `. ` ` ` `"from L to R is "` `, ` ` ` `suminRange(` `$l` `, ` `$r` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum of Natural numbers from L to R is 14

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:

- Sum of all odd natural numbers in range L and R
- Sum of range in a series of first odd then even natural numbers
- Count of natural numbers in range [L, R] which are relatively prime with N
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Greatest divisor which divides all natural number in range [L, R]
- LCM of First n Natural Numbers
- Sum of first n natural numbers
- Natural Numbers
- Sum of cubes of even and odd natural numbers
- Average of first n even natural numbers
- Sum of fifth powers of the first n natural numbers
- Repeated sum of first N natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of squares of first n natural numbers
- Sum of cubes of first n odd natural numbers
- Sum of kth powers of first n natural numbers
- Sum of first N natural numbers which are divisible by X or Y
- Sum of first N natural numbers which are not powers of K
- Sum of sum-series of first N Natural 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.