Given an integer **N** where **1 ≤ N ≤ 10 ^{5}**, the task is to find the number of positive integers less than or equal to

**N**that have an odd number of digits without leading zeros.

**Examples:**

Input:N = 11

Output:9

1, 2, 3, …, 8 and 9 are the numbers ≤ 11

with odd number of digits.

Input:N = 893

Output:803

**Naive approach:** Traverse from **1** to **N** and for each number check if it contains odd digits or not.

**Efficient approach:** For the values:

- When
**N < 10**then the count of valid numbers will be**N**. - When
**N / 10 < 10**then**9**. - When
**N / 100 < 10**then**9 + N – 99**. - When
**N / 1000 < 10**then**9 + 900**. - When
**N / 10000 < 10**then**909 + N – 9999**. - Otherwise
**90909**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of ` `// positive integers less than or equal ` `// to N that have odd number of digits ` `int` `odd_digits(` `int` `n) ` `{ ` ` ` `if` `(n < 10) ` ` ` `return` `n; ` ` ` `else` `if` `(n / 10 < 10) ` ` ` `return` `9; ` ` ` `else` `if` `(n / 100 < 10) ` ` ` `return` `9 + n - 99; ` ` ` `else` `if` `(n / 1000 < 10) ` ` ` `return` `9 + 900; ` ` ` `else` `if` `(n / 10000 < 10) ` ` ` `return` `909 + n - 9999; ` ` ` `else` ` ` `return` `90909; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 893; ` ` ` ` ` `cout << odd_digits(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// positive integers less than or equal ` `// to N that have odd number of digits ` `static` `int` `odd_digits(` `int` `n) ` `{ ` ` ` `if` `(n < ` `10` `) ` ` ` `return` `n; ` ` ` `else` `if` `(n / ` `10` `< ` `10` `) ` ` ` `return` `9` `; ` ` ` `else` `if` `(n / ` `100` `< ` `10` `) ` ` ` `return` `9` `+ n - ` `99` `; ` ` ` `else` `if` `(n / ` `1000` `< ` `10` `) ` ` ` `return` `9` `+ ` `900` `; ` ` ` `else` `if` `(n / ` `10000` `< ` `10` `) ` ` ` `return` `909` `+ n - ` `9999` `; ` ` ` `else` ` ` `return` `90909` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `n = ` `893` `; ` ` ` ` ` `System.out.println(odd_digits(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the number of ` `# positive integers less than or equal ` `# to N that have odd number of digits ` `def` `odd_digits(n) : ` ` ` ` ` `if` `(n < ` `10` `) : ` ` ` `return` `n; ` ` ` `elif` `(n ` `/` `10` `< ` `10` `) : ` ` ` `return` `9` `; ` ` ` `elif` `(n ` `/` `100` `< ` `10` `) : ` ` ` `return` `9` `+` `n ` `-` `99` `; ` ` ` `elif` `(n ` `/` `1000` `< ` `10` `) : ` ` ` `return` `9` `+` `900` `; ` ` ` `elif` `(n ` `/` `10000` `< ` `10` `) : ` ` ` `return` `909` `+` `n ` `-` `9999` `; ` ` ` `else` `: ` ` ` `return` `90909` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `893` `; ` ` ` ` ` `print` `(odd_digits(n)); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// positive integers less than or equal ` `// to N that have odd number of digits ` `static` `int` `odd_digits(` `int` `n) ` `{ ` ` ` `if` `(n < 10) ` ` ` `return` `n; ` ` ` `else` `if` `(n / 10 < 10) ` ` ` `return` `9; ` ` ` `else` `if` `(n / 100 < 10) ` ` ` `return` `9 + n - 99; ` ` ` `else` `if` `(n / 1000 < 10) ` ` ` `return` `9 + 900; ` ` ` `else` `if` `(n / 10000 < 10) ` ` ` `return` `909 + n - 9999; ` ` ` `else` ` ` `return` `90909; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `n = 893; ` ` ` ` ` `Console.WriteLine(odd_digits(n)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

803

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
- Find three integers less than or equal to N such that their LCM is maximum
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count number of integers less than or equal to N which has exactly 9 divisors
- Smallest odd digits number not less than N
- Find maximum product of digits among numbers less than or equal to N
- Find K distinct positive odd integers with sum N
- Greatest number less than equal to B that can be formed from the digits of A
- Highest and Smallest power of K less than and greater than equal to N respectively
- Count positive integers with 0 as a digit and maximum 'd' digits
- Count of digits after concatenation of first N positive integers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find Largest Special Prime which is less than or equal to a given number
- Find all powers of 2 less than or equal to a given number
- Print a number strictly less than a given number such that all its digits are distinct.
- Find all factorial numbers less than or equal to n
- Find unique pairs such that each element is less than or equal to N
- Find Multiples of 2 or 3 or 5 less than or equal to N
- Find all possible subarrays having product less than or equal to K
- Smallest even digits number not less than N

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.