Sum of all even numbers in range L and R | Function to return the sum of all natural numbers ; Function to return sum of even numbers in range L and R ; driver program
function sumNatural ( n ) { let sum = Math . floor ( n * ( n + 1 ) ) ; return sum ; } function sumEven ( l , r ) { return sumNatural ( Math . floor ( r / 2 ) ) - sumNatural ( Math . floor ( l - 1 ) / 2 ) ; } let l = 2 , r = 5 ; document . write ( " " + sumEven ( l , r ) ) ;