Home  >  Article  >  Backend Development  >  Write a program using C++ to find the Nth number consisting of odd digits

Write a program using C++ to find the Nth number consisting of odd digits

WBOY
WBOYforward
2023-09-14 11:09:081406browse

Write a program using C++ to find the Nth number consisting of odd digits

C has a huge list of functions to solve math problems. One of the mathematical functions is to find the Nth odd number of digits using code. This article will describe the complete method of finding an odd number of digits and understand what an odd number is and what a number consists of an odd number of digits.

Find the Nth number composed of odd digits

Odd numbers will have a remainder when divided by 2, 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 methods:

Method 1 - Check each natural number, Determine if it is an odd number and count each odd number until the count equals n, if the number found is an even number, do not count, i.e. skip the even numbers and count the odd numbers and give the Nth number found.

This method of finding the Nth number consisting of odd number of digits may be simple as we just check each number and count the odd ones, but in terms of computer programming this method takes a lot of time to Complete this task.

Method 2 - The last digit of each number consisting of odd digits can be 1, 3, 5, 7, 9, so they are odd numbers. So we first check if the Nth number is 1, 3, 5, 7, 9 and if so we get the answer; otherwise we move to other possible numbers i.e. 11, 13, 15, 17, 19 and then 21, 23, 25, 27, 29. This forms a pattern: 1 * 10 {last possible number}.

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 the 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 input, we get 9 as output)

The above code is the C code for finding the Nth number consisting of only odd digits. To understand this code, let us break it down and understand each part of it to understand the complete code.

Code Explanation

Step 1 - Get n from user and initialize required variables.

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 the variables cnt to count and ans to store the answers. At the same time, we use cin to get input from the user and initialize an array with the first possible number.

Second step - Check if the Nth number is among the initial possible numbers and store these 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 code, we check if the Nth number is available in the first possible number, stored in the array and push the number present in the array to the queue, if in the If the Nth number is found among a possible number, then give the output

Step 3 - Find the Nth number among the next possible numbers, if the Nth number is not found number, the number is changed in the queue.

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 pop each number from the queue and generate the next possible number using the formula { x * 10 last odd number } and check if the value of cnt is equal to n.

Conclusion

In this article, we have a problem: how to find the Nth odd number consisting of odd digits, and found two methods to solve it. The first method is simple, just check each number and skip the even numbers, but it takes longer to calculate.

The second method is to use a queue to store the odd numbers in it and use the above formula to find the next possible number. The complexity of this method is O(n).

We have written a program in C to find the Nth number consisting of only odd digits; we can write this program in any other language like C, Python, Java or other programming languages . I hope you found this article helpful in solving your problem.

The above is the detailed content of Write a program using C++ to find the Nth number consisting of odd digits. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete