Sum of all natural numbers in range L to R - GeeksforGeeks

05 Apr, 2021
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 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++
Java
Python3
C#
PHP
Javascript
// 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 numbersint sumNatural(int n){    int sum = (n * (n + 1)) / 2;    return sum;} // Function to return the sum// of all numbers in range L and Rint suminRange(int l, int r){    return sumNatural(r) - sumNatural(l - 1);} // Driver Codeint main(){    int l = 2, r = 5;    cout << "Sum of Natural numbers from L to R is "         << suminRange(l, r);     return 0;}
// Java program to print the sum// of all numbers in range L and R class GFG{// Function to return the sum of// all natural numbersstatic int sumNatural(int n){    int sum = (n * (n + 1)) / 2;    return sum;} // Function to return the sum// of all numbers in range L and Rstatic int suminRange(int l, int r){    return sumNatural(r) - sumNatural(l - 1);} // Driver Codepublic 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
# Python3 program to print the sum  of# all numbers in range L and R # Function to return the sum of all natural numbersdef sumNatural(n):     sum = (n*(n+1))//2     return sum # Function to return the sum# of all numbers in range L and Rdef suminRange(l, r):    return sumNatural(r) - sumNatural(l-1) #  Driver Codel =2; r= 5print("Sum of Natural numbers from L to R is ",suminRange(l, r)) # This code is contributed by Shrikant13
// C# program to print the sum// of all numbers in range L and Rusing System; class GFG{// Function to return the sum// of all natural numbersstatic int sumNatural(int n){    int sum = (n * (n + 1)) / 2;    return sum;} // Function to return the sum// of all numbers in range L and Rstatic int suminRange(int l, int r){    return sumNatural(r) -           sumNatural(l - 1);} // Driver Codestatic 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
<?php// PHP program to print the sum// of all numbers in range L and R // Function to return the sum of// all natural numbersfunction sumNatural($n){    $sum = ($n * ($n + 1)) / 2;    return $sum;} // Function to return the sum// of all numbers in range L and Rfunction 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?>
<script>// JavaScript program to print the sum// of all numbers in range L and R // Function to return the sum of// all natural numbersfunction sumNatural(n){    sum = (n * (n + 1)) / 2;    return sum;} // Function to return the sum// of all numbers in range L and Rfunction suminRange(l, r){    return sumNatural(r) -           sumNatural(l - 1);} // Driver Codelet l = 2;let r = 5;document.write("Sum of Natural numbers from L to R is "+             suminRange(l, r)); // This code is contributed by sravan kumar gottumukkalan </script>
Sum of Natural numbers from L to R is 14
 
Mithun Kumar
jit_t
shrikanth13
sravankumar8128
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Program to add two binary strings
Find Union and Intersection of two unsorted arrays
Count ways to reach the n'th stair
Add two numbers without using arithmetic operators
Program to convert a given number to words