Number of n digit numbers that do not contain 9


Suppose you have a given number N. Now, you want to find those numbers of N digit which do not contain 9. This question can be solved by permutation and combination mathematically. In this article, we will find such numbers and count them using C++.

Suppose you have to find 1-digit numbers which do not contain 9. So, these numbers are (0 – 8). There are in total 8 such numbers. Similarly, we have to find the count of N digit such numbers.

Here, we will be given the value of N and then we have to find the count of all N digit numbers which don’t contain 9.

Input Output Scenarios

We are given N. The number of N digit numbers that do not contain 9 is the output.

Input: N = 2
Output: 72
Input: N = 3
Output: 648

For N = 2, possible numbers which do not contain the digit 9 are all the numbers except 90 – 99 and those which have 9 at the ones place. The number of such cases is 72.

Suppose there are Z number of N digit numbers. Using permutation, the total number of N digit numbers formed is equal to 9 * 10N – 1. This is because each digit will be constituted of numbers between (0 – 9), except for having 0 as its first digit.

Now, in our question, we have to find such numbers which do not contain the digit 9. This means one number that is, 9 needs to be excluded. So, the formula becomes 8 * 9N – 1.

Using the pow() Function

The pow() function accepts two arguments as parameters, raises the first argument to the power of the second and returns the result. We will use the pow() function to calculate 9N – 1

#include <iostream>
#include <cmath>
using namespace std;
int possibleNumbers(int N){
   return 8 * pow(9, N - 1);
}
int main() {
   int N = 3;
   cout << "Number of " << N << " digit number which do not contain 9 are " <<
   possibleNumbers(N);
   return 0;
}

Output

Number of 3 digit number which do not contain 9 are 648

Using Iteration and if Statement

Using the pow() function involves various calculations which may decrease the efficiency of the program. Instead of this, we can try an efficient approach which involves use of for loop and if statement for doing the exponential operations.

Example

#include <iostream>
#include <cmath>
using namespace std;
long long power(int base, int exp) {
   int a = 1;
   for (int j = 1; j <= exp; ++j) {
      a *= base;
   }
   return a;
}
int possibleNumbers(int N) {
   int x = power(9, N - 1);
   return 8 * x;
}
int main() {
   int N = 2;
   cout << "Number of " << N << " digit number which do not contain 9 are " <<
   possibleNumbers(N);
   return 0;
}

Output

Number of 2 digit number which do not contain 9 are 72

Conclusion

We have discussed different ways to find the number of N digit number which do not contain 9. The first approach is to use the pow() function of the <cmath> library. The second approach is to use for loop and if statement to calculate the exponential operations. The latter one is more efficient because it decreases the time complexity.

Updated on: 05-Jan-2024

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements