# Nth number made up of odd digits only

Given an integer N, the task is to find the Nth number made up of odd digits (1, 3, 5, 7, 9) only.
First few numbers made up of odd digits are 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 31, …

Examples:

Input: N = 7
Output: 13
1, 3, 5, 7, 9, 11, 13
13 is the 7th number in the series

Input: N = 10
Output: 19

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach 1 (Simple) : Starting from 1, keep checking if the number is made up of only odd digits (1, 3, 5, 7, 9) and stop when nth such number is found.

Below is the implementation of the above approach:

## C++

 `// C++ program to find nth number made up of odd digits only ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return nth number made up of odd digits only ` `int` `findNthOddDigitNumber(``int` `n) ` `{ ` ` `  `    ``// Variable to keep track of how many ` `    ``// such elements have been found ` `    ``int` `count = 0; ` `    ``for` `(``int` `i = 1;; i++) { ` `        ``int` `num = i; ` `        ``bool` `isMadeOfOdd = ``true``; ` ` `  `        ``// Checking each digit of the number ` `        ``while` `(num != 0) { ` ` `  `            ``// If 0, 2, 4, 6 or 8 is found ` `            ``// then the number is not made up of odd digits ` `            ``if` `(num % 10 == 0 ` `                ``|| num % 10 == 2 ` `                ``|| num % 10 == 4 ` `                ``|| num % 10 == 6 ` `                ``|| num % 10 == 8) { ` `                ``isMadeOfOdd = ``false``; ` `                ``break``; ` `            ``} ` ` `  `            ``num = num / 10; ` `        ``} ` ` `  `        ``// If the number is made up of odd digits only ` `        ``if` `(isMadeOfOdd == ``true``) ` `            ``count++; ` ` `  `        ``// If it is the nth number ` `        ``if` `(count == n) ` `            ``return` `i; ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` `    ``cout << findNthOddDigitNumber(n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find nth number  ` `// made up of odd digits only  ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `    ``// Function to return nth number made up of odd digits only  ` `static` `int` `findNthOddDigitNumber(``int` `n)  ` `{  ` ` `  `    ``// Variable to keep track of how many  ` `    ``// such elements have been found  ` `    ``int` `count = ``0``;  ` `    ``for` `(``int` `i = ``1``;; i++) {  ` `        ``int` `num = i;  ` `        ``boolean` `isMadeOfOdd = ``true``;  ` ` `  `        ``// Checking each digit of the number  ` `        ``while` `(num != ``0``) {  ` ` `  `            ``// If 0, 2, 4, 6 or 8 is found  ` `            ``// then the number is not made up of odd digits  ` `            ``if` `(num % ``10` `== ``0`  `                ``|| num % ``10` `== ``2`  `                ``|| num % ``10` `== ``4`  `                ``|| num % ``10` `== ``6`  `                ``|| num % ``10` `== ``8``) {  ` `                ``isMadeOfOdd = ``false``;  ` `                ``break``;  ` `            ``}  ` ` `  `            ``num = num / ``10``;  ` `        ``}  ` ` `  `        ``// If the number is made up of odd digits only  ` `        ``if` `(isMadeOfOdd == ``true``)  ` `            ``count++;  ` ` `  `        ``// If it is the nth number  ` `        ``if` `(count == n)  ` `            ``return` `i;  ` `    ``}  ` `}  ` ` `  `// Driver Code  ` `     `  `    ``public` `static` `void` `main (String[] args) { ` `    ``int` `n = ``10``;  ` `    ``System.out.println (findNthOddDigitNumber(n));  ` `         `  `    ``} ` `//This code is contributed by ajit     ` `} `

## Python3

 `# Python 3 program to find nth number  ` `# made up of odd digits only ` ` `  `# Function to return nth number made ` `# up of odd digits only ` `def` `findNthOddDigitNumber(n) : ` `     `  `    ``# Variable to keep track of how many ` `    ``# such elements have been found ` `    ``count ``=` `0` `     `  `    ``i ``=` `1` `    ``while` `True` `: ` `        ``num ``=` `i ` `        ``isMadeOfOdd ``=` `True` `         `  `        ``# Checking each digit of the number ` `        ``while` `num !``=` `0` `: ` `             `  `            ``# If 0, 2, 4, 6 or 8 is found ` `            ``# then the number is not made ` `            ``# up of odd digits ` `            ``if` `(num ``%` `10` `=``=` `0` `or` `num ``%` `10` `=``=` `2` `or`  `                ``num ``%` `10` `=``=` `4` `or` `num ``%` `10` `=``=` `6` `or` `                ``num ``%` `10` `=``=` `8``) : ` `                     `  `                    ``isMadeOfOdd ``=` `False` `                    ``break` `         `  `            ``num ``/``=` `10` `     `  `        ``# If the number is made up of ` `        ``# odd digits only ` `        ``if` `isMadeOfOdd ``=``=` `True` `: ` `            ``count ``+``=` `1` `     `  `        ``# If it is the nth number ` `        ``if` `count ``=``=` `n : ` `            ``return` `i ` `     `  `        ``i ``+``=` `1` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``n ``=` `10` `     `  `    ``# Function call ` `    ``print``(findNthOddDigitNumber(n)) ` `     `  `# This code is contributed by Ryuga `

## C#

 `// C# program to find nth number  ` `// made up of odd digits only  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Function to return nth number  ` `// made up of odd digits only  ` `static` `int` `findNthOddDigitNumber(``int` `n)  ` `{  ` ` `  `    ``// Variable to keep track of  ` `    ``// how many such elements have ` `    ``// been found  ` `    ``int` `count = 0;  ` `    ``for` `(``int` `i = 1;; i++) ` `    ``{  ` `        ``int` `num = i;  ` `        ``bool` `isMadeOfOdd = ``true``;  ` ` `  `        ``// Checking each digit  ` `        ``// of the number  ` `        ``while` `(num != 0)  ` `        ``{  ` ` `  `            ``// If 0, 2, 4, 6 or 8 is found  ` `            ``// then the number is not made  ` `            ``// up of odd digits  ` `            ``if` `(num % 10 == 0 || num % 10 == 2 ||  ` `                ``num % 10 == 4 || num % 10 == 6 ||  ` `                ``num % 10 == 8) ` `            ``{  ` `                ``isMadeOfOdd = ``false``;  ` `                ``break``;  ` `            ``}  ` ` `  `            ``num = num / 10;  ` `        ``}  ` ` `  `        ``// If the number is made up of ` `        ``// odd digits only  ` `        ``if` `(isMadeOfOdd == ``true``)  ` `            ``count++;  ` ` `  `        ``// If it is the nth number  ` `        ``if` `(count == n)  ` `            ``return` `i;  ` `    ``}  ` `}  ` ` `  `// Driver Code  ` `static` `public` `void` `Main () ` `{ ` `    ``int` `n = 10;  ` `    ``Console.WriteLine(findNthOddDigitNumber(n));  ` `} ` `} ` ` `  `// This code is contributed  ` `// by Ajit Deshpal `

## PHP

Output:

```19
```

Approach 2 (Queue Based): The idea is to generate all numbers (smaller than n) containing odd digits only. How to generate all numbers smaller than n with odd digits? We use queue for this. Initially we push ‘1’, ‘3’, ‘5’, ‘7’ and ‘9’ to the queue. Then we run a loop while count of processed elements is smaller than n. We pop an item one by one and for every popped item x, we generate next numbers x*10 + 1, x*10 + 3, x*10 + 5, x*10 + 7 and x*10 + 9. We enqueue these new numbers. Time complexity of this approach is O(n)

Please refer below post for implementation of this approach.
Count of Binary Digit numbers smaller than N

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.