Given two numbers L and R, the task is to count the number of odd numbers in the range L to R.

**Examples:**

Input:l = 3, r = 7

Output:3 2

Count of odd numbers is 3 i.e. 3, 5, 7

Count of even numbers is 2 i.e. 4, 6

Input:l = 4, r = 8

Output:2

**Approach:** Total numbers in the range will be **(R – L + 1)** i.e. N.

- If N is even then the count of both odd and even numbers will be
**N/2**. - If N is odd,
- If L or R is odd, then the count of odd number will be
**N/2 + 1**and even numbers =**N – countofOdd**. - Else, count of odd numbers will be
**N/2**and even numbers =**N – countofOdd**.

- If L or R is odd, then the count of odd number will be

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Return the number of odd numbers ` `// in the range [L, R] ` `int` `countOdd(` `int` `L, ` `int` `R){ ` ` ` ` ` `int` `N = (R - L) / 2; ` ` ` ` ` `// if either R or L is odd ` ` ` `if` `(R % 2 != 0 || L % 2 != 0) ` ` ` `N += 1; ` ` ` ` ` `return` `N; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `L = 3, R = 7; ` ` ` `int` `odds = countOdd(L, R); ` ` ` `int` `evens = (R - L + 1) - odds; ` ` ` ` ` `cout << ` `"Count of odd numbers is "` `<< odds << endl; ` ` ` `cout << ` `"Count of even numbers is "` `<< evens << endl; ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `class` `GFG { ` ` ` ` ` `// Return the number of odd numbers ` ` ` `// in the range [L, R] ` ` ` `static` `int` `countOdd(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` `int` `N = (R - L) / ` `2` `; ` ` ` ` ` `// if either R or L is odd ` ` ` `if` `(R % ` `2` `!= ` `0` `|| L % ` `2` `!= ` `0` `) ` ` ` `N++; ` ` ` ` ` `return` `N; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `L = ` `3` `, R = ` `7` `; ` ` ` ` ` `int` `odds = countOdd(L, R); ` ` ` `int` `evens = (R - L + ` `1` `) - odds; ` ` ` `System.out.println(` `"Count of odd numbers is "` `+ odds); ` ` ` `System.out.println(` `"Count of even numbers is "` `+ evens); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the ` `# above approach ` ` ` `# Return the number of odd numbers ` `# in the range [L, R] ` `def` `countOdd(L, R): ` ` ` ` ` `N ` `=` `(R ` `-` `L) ` `/` `/` `2` ` ` ` ` `# if either R or L is odd ` ` ` `if` `(R ` `%` `2` `!` `=` `0` `or` `L ` `%` `2` `!` `=` `0` `): ` ` ` `N ` `+` `=` `1` ` ` ` ` `return` `N ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `L ` `=` `3` ` ` `R ` `=` `7` ` ` ` ` `odds ` `=` `countOdd(L, R) ` ` ` `evens ` `=` `(R ` `-` `L ` `+` `1` `) ` `-` `odds ` ` ` `print` `(` `"Count of odd numbers is"` `, odds) ` ` ` `print` `(` `"Count of even numbers is"` `, evens) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Return the number of odd numbers ` ` ` `// in the range [L, R] ` ` ` `static` `int` `countOdd(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` `int` `N = (R - L) / 2; ` ` ` ` ` `// if either R or L is odd ` ` ` `if` `(R % 2 != 0 || L % 2 != 0) ` ` ` `N++; ` ` ` ` ` `return` `N; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `L = 3, R = 7; ` ` ` ` ` `int` `odds = countOdd(L, R); ` ` ` `int` `evens = (R - L + 1) - odds; ` ` ` `Console.WriteLine(` `"Count of odd numbers is "` `+ odds); ` ` ` `Console.WriteLine(` `"Count of even numbers is "` `+ evens); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the above approach ` ` ` `// Return the number of odd numbers ` `// in the range [L, R] ` `function` `countOdd(` `$L` `, ` `$R` `) ` `{ ` ` ` `$N` `= (` `$R` `- ` `$L` `) / 2; ` ` ` ` ` `// if either R or L is odd ` ` ` `if` `(` `$R` `% 2 != 0 || ` `$L` `% 2 != 0) ` ` ` `$N` `++; ` ` ` ` ` `return` `$N` `; ` `} ` ` ` `// Driver code ` `$L` `= 3; ` `$R` `= 7; ` ` ` `$odds` `= countOdd(` `$L` `, ` `$R` `); ` `$evens` `= (` `$R` `- ` `$L` `+ 1) - ` `$odds` `; ` `echo` `"Count of odd numbers is "` `. ` `$odds` `. ` `"\n"` `; ` `echo` `"Count of even numbers is "` `. ` `$evens` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Count of odd numbers is 3 Count of even numbers is 2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Sum of elements in range L-R where first half and second half is filled with odd and even numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Print even and odd numbers in a given range using recursion
- Sum of range in a series of first odd then even natural numbers
- Check whether XOR of all numbers in a given range is even or odd
- Generate an array of given size with equal count and sum of odd and even numbers
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Python program to count Even and Odd numbers in a List
- Count set bits in the Kth number after segregating even and odd from N natural numbers
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Count of numbers in range which are divisible by M and have digit D at odd places
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Check if count of even divisors of N is equal to count of odd divisors
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Program to find count of numbers having odd number of divisors in given range
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.