# Find odd numbers between a closed range

Suppose we have been given two numbers let us say x and y.we have to find all odd number which lies between x and y(both inclusive)

Let us see a sample code after that we will understand it.

``````class Countodd {
static int countOdd(int L, int R)
{
int N = (R - L) / 2;

if (R % 2 != 0 || L % 2 != 0)
N++;

return N;
}

public static void main(String[] args)
{
int L = 3, R = 7;

int odds = countOdd(L, R);
System.out.println("Count of odd numbers is " + odds);

}
}
``````

In the above example we have taken a function name countOdd which count the number of odd numbers within the range.

It is the most optimal solution as we do not have to traverse the whole range using loop.

Firstly we have divided the range by 2 to get the number of odd values.but only this method is not enough to count the total numbers.so after that we check whether the staring or ending values are even or odd.if odd then we will add 1 to our previous result.

3,280 points
7 4 2