Sum of all odd natural numbers in range L and R | Java program to print the sum of all numbers in range L and R ; Function to return the sum of all odd natural numbers ; Function to return the sum of all odd numbers in range L and R ; Driver Code
import java . io . * ; class GFG { static int sumOdd ( int n ) { int terms = ( n + 1 ) / 2 ; int sum = terms * terms ; return sum ; } static int suminRange ( int l , int r ) { return sumOdd ( r ) - sumOdd ( l - 1 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . print ( " Sum ▁ of ▁ odd ▁ natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + suminRange ( l , r ) ) ; } }