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
using System ; class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } static int suminRange ( int l , int r ) { return sumNatural ( r ) - sumNatural ( l - 1 ) ; } static public void Main ( ) { int l = 2 , r = 5 ; Console . WriteLine ( " Sum ▁ of ▁ Natural ▁ numbers ▁ " + " from ▁ L ▁ to ▁ R ▁ is ▁ " + suminRange ( l , r ) ) ; } }