Sum of all natural numbers in range L to R | C ++ program to print the sum of all numbers in range L and R ; Function to return the sum of all natural numbers ; Function to return the sum of all numbers in range L and R ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } int suminRange ( int l , int r ) { return sumNatural ( r ) - sumNatural ( l - 1 ) ; } int main ( ) { int l = 2 , r = 5 ; cout << " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " << suminRange ( l , r ) ; return 0 ; }