Find the Nth Number Made Up of only Odd Digits using C++

C++Server Side ProgrammingProgramming

C++ has a huge list of functions to solve mathematical issues. One of the mathematical functions is to find Nth odd digits using codes. This article will describe the complete approach of finding the odd Nth number and understand what odd numbers are and what numbers are made up of odd digits.

Finding the Nth Number Made up of Odd Digits Only

Odd numbers give remainder on dividing by two, so the first few odd numbers are 1,3,5,7,9,11,13,15,17,19...

To find the required number, we have two approaches here −

Approach 1 − Check every natural number, whether it is an odd number or not and do count for every odd number until the count becomes equal to n, and do not count if the number found even, i.e., skipping the even number and counting the odd numbers and giving the required Nth number found.

This approach of finding Nth number made up of odd digits can be simple as we are just checking every number and taking count of odd numbers, but in terms of computer programming, this approach takes much time to complete this task.

Approach 2 − Every number made up of odd digits can have 1,3,5,7,9 in the last, so they are odd numbers. So we first check whether Nth number is 1,3,5,7,9 if yes, then we get the answer; otherwise, we move to other possible numbers, which are 11.13,15,17,19 and then comes 21,23,25,27,29. This forming a pattern 1 * 10 + {last possible numbers}.

Example

Last possible numbers are 1,3,5,7,9
Next possible numbers can be found by
1 * 10 + 1 =11
1 * 10 + 3 = 13
1 * 10 + 5 = 15
1 * 10 + 7 = 17
1* 10 + 9 = 19
i.e 11,13,15,17,19

Sample Code

First, let's see what code looks like −

#include<bits/stdc++.h>
using namespace std;
int main(){
    queue<int> q;
    int cnt = 0, ans = 0;
    int n;
    cin >> n;
    int a[5]={1,3,5,7,9};
    for(int i = 0; i < 5;i++){
        cnt++;
        if(cnt == n)
            ans = a[i];
        q.push(a[i]);
    }
    if(ans)
        cout << ans << "\n";
    else{
        while(true){
            int x = q.front();
            q.pop();
            for(int j = 0; j < 5; j++) {
                int temp = x * 10 + a[j];
                q.push(temp);
                cnt++;
                if(cnt == n)
                    ans = temp;
            }
            if(ans)
                break;
        }
        cout << ans << "\n";
    }
    return 0;
}

Output

9

(When we provide 5 as an input, then we get 9 as an output)

The code above is the C++ code to find Nth number made up of odd digits only. To understand this code, let's break this code and understand each part of it to understand complete code.

Explanation of Codes

Step 1 − Taking n from the user and initializing the required variable.

int main() {
   queue<int> q;
   int cnt = 0, ans = 0;
   int n;
   cin >> n;
   int a[5]={1,3,5,7,9};

Here we create a queue and initialize variable cnt to keep count and ans to store answers. Also, we are taking input from the user using cin and initializing an array with the first possible numbers.

Step 2 − To check whether the Nth number is found in the initial possible numbers and storing those numbers in the queue.

for(int i = 0; i < 5;i++){
   cnt++;
   if(cnt == n)
      ans = a[i];
      q.push(a[i]);
   }
   if(ans)
      cout << ans << "\n";

In the above codes, we are checking whether the Nth number is available in the first possible numbers, stored in an array and pushing queue with numbers present in the array, and giving output if the Nth number is found in the first possible numbers.

Step 3 − Find the Nth number in the next possible numbers and changing numbers in the queue if the Nth number is not found.

while(true) {
   int x = q.front();
   q.pop();
   for(int j = 0; j < 5; j++) {
      int temp = x * 10 + a[j];
      q.push(temp);
      cnt++;
      if(cnt == n)
         ans = temp;
      }
      if(ans)
         break;
   }
   cout << ans << "\n";
}

Finally, we are popping out every number from the queue and making the next possible number from that using formula { x * 10 + last odd number } and checking whether the value of cnt becomes equal to n.

Conclusion

In this article, we had a problem: how to find Nth odd number made up of odd digits only, and we found two approaches to do so. The first approach was simple to understand as it is just checking each number and skipping even numbers, but it was taking time in computing.

The second approach was using a queue and storing odd numbers in it and finding the next possible numbers with a formula, we have seen above. The complexity of this approach is O(n).

We made a program in C++ to find Nth numbers made up of odd digits only; we can do this program in any other language such as C, python, java, or any other programming language. Hope you found this article helpful as it gives complete knowledge of the approach to solve the problem.

raja
Published on 23-Nov-2021 10:14:13
Advertisements