
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count n digit numbers not having a particular digit in C++
We are given a number let’s say, num and a total number of digits stored in an integer type variable let’s say, digi and the task is to calculate the count of those n digits numbers that can be formed where the given digit is not there.
Input − n = 2, digit = 2
Output − count is 153
Explanation − count of all two digit numbers(n) not having digit 2 is 153 as 10, 11, 13, 14, 15, 16, 17, 18, 19, 30, 31, 33, 34,.......,etc.
Input − n = 3, digit = 3
Output − count is 2187
Explanation − count of all three digit numbers(n) not having digit 3 is 2187 as 10, 11, 13, 14, 15, 16, 17, 18, 19, 30, 31, 33, 34,.......,etc.
Approach used in the below program is as follows
Input the number ‘n’ and the digit as an integer variables
Pass these variables to a function that will perform a count operation
Set two variables min and max value that ‘n’ can reach to. For example, a 2-digit number starts with a minimum value of 10 and ends till 99, similarly, 3-digit numbers start with a minimum of 100 till 999.
Start the loop from min to max
Inside the loop, start while till ‘n’ is greater than 0
Check if the number is there or not. If the number is there don’t perform any operation and if the number isn’t there increase the count by 1.
Example
#include<bits/stdc++.h> using namespace std; int count(int n, int digit){ int r =0; int count = 0; //calculate the min and max of the given number int min = (int)(pow(10, n-1)); int max = (int)(pow(10, n)); //start the loop till max value start from min for(int i=min; i<max; i++){ int a=i; int f=0; //while a is greater than 0 while(a>0){ r=a%10; a=a/10; if(r==digit){ f++; } if(f==0){ count++; } } } return count; } int main(){ int n = 2, digit = 2; cout<<"Count of "<<n<< digit numbers not having a particular digit "<<digit<<" is :"<<count(n, digit); return 0; }
Output
If we run the above code we will get the following output −
Count of 2 digit numbers not having a particular digit 2 is :153
- Related Articles
- Count numbers having 0 as a digit in C++
- Count of Binary Digit numbers smaller than N in C++
- Count n digit numbers divisible by given number in C++
- Python – Sort by a particular digit count in elements
- Count numbers from 1 to n that have 4 as a digit in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Number of n digit stepping numbers in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Count of all N digit numbers such that num + Rev(num) = 10^N - 1 in C++
- Count ‘d’ digit positive integers with 0 as a digit in C++
- Print all n-digit strictly increasing numbers in C++
- Numbers At Most N Given Digit Set in C++
- Largest Even and Odd N-digit numbers in C++
