
- 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 positive integers with 0 as a digit and maximum ‘d' digits in C++
We are given a number d which represents the number of digits. The goal is to find the count of positive integers with 0 as a digit and have maximum d digits. Count all 1 digit, 2 digit, 3 digit….d digit positive numbers containing at least one 0.
We will first find numbers the count of numbers that have d digits with at least one 0. Let’s say d=3. To make a 3-digit number with at least one 0, possible ways are −
Here d1 can have 1 to 9 : 9 ways d2 can have 0-9 : 10 ways d3 can have 0-9 : 10 ways Total numbers possible: 9 x 10 x 10 = 9 x 102 For d digits, count of numbers: 9 x 10d-1 For d digits, numbers without any 0 are : 9d Total numbers having d digits with at least one 0 = 9 x 10d-1 - 9d = 9 x ( 10d-1 - 9d-1 )
Let us understand with examples
Input − d=4
Output − Count of positive integers with 0 as a digit and maximum 'd' digits are − 2619
Explanation − x digit numbers with at least one 0 −
1 digit numbers : 0 2 digit numbers : 9 3 digit numbers : 171 4 digit numbers: 2439 Total= 9+171+2439 = 2619
Input − d=1
Output − Count of positive integers with 0 as a digit and maximum 'd' digits are − 0
Explanation − 1 to 9 no number has 0 as a digit.
The approach used in the below program is as follows
We will use two approaches. A first naive approach using a for a loop. Start traversing from 1 digit upto d digits and calculate numbers using the formula mentioned above. Add returned value to count.
Take the integer d for digits.
Function total_count(int d)) takes a number of digits d and returns a count of numbers with d digits having at least one 0.
Calculate such numbers as temp=9*(pow(10,d-1) - pow(9,d-1));
Return temp.
Function maximum_d(int d) takes the maximum number of digits d and returns count of numbers upto d digits having at least one 0.
Traverse using a loop starting from 1 digit numbers then 2 and so on till d.
For each d calculate numbers as total_count(i). Add this to count.
At last we will get the total count.
Return count as result.
Efficient Approach
In this approach, we will calculate the count by observing the G.P formed for the above calculations.
Solution is 9 x (10d-1 - 9d-1) = 9 x (10d - 1)- 9 x (9d-1) = 9 x (10i - 1) - 9 x (9i - 1) ( 1<=i<=d ) = g.p 1 - g.p 2 = 9x(10d-1)/(10-1) - 9x(9d-1)/(9-1) = (10d-1)- (9/8)*(9d-1)
Take d as the maximum number of digits.
Function maximum_d(int d) takes the maximum number of digits d and returns a count of numbers up to d digits having at least one 0.
Using above formula calculate temp_1 as 9*((pow(10,d)-1)/9).
Calculate temp_2 as 9*((pow(9,d)-1)/8).
Set count = temp_1 - temp_2.
Return count as result.
Example (naive approach)
#include<bits/stdc++.h> using namespace std; int total_count(int d){ int temp = 9*(pow(10,d-1) - pow(9,d-1)); return temp; } int maximum_d(int d){ int count = 0; for (int i=1; i<=d; i++){ count = count + total_count(i); } return count; } int main(){ int d = 5; cout<<"Count of positive integers with 0 as a digit and maximum 'd' digits are: "<<maximum_d(d) << endl; return 0; }
Output
If we run the above code it will generate the following output −
Count of positive integers with 0 as a digit and maximum 'd' digits are: 33570
Example (Efficient approach)
#include<bits/stdc++.h> using namespace std; int maximum_d(int d){ int temp_1 = 9*((pow(10,d)-1)/9); int temp_2 = 9*((pow(9,d)-1)/8); int count = temp_1 - temp_2; return count; } int main(){ int d = 4; cout<<"Count of positive integers with 0 as a digit and maximum 'd' digits are: "<<maximum_d(d) << endl; return 0; }
Output
If we run the above code it will generate the following output −
Count of positive integers with 0 as a digit and maximum 'd' digits are: 2619
- Related Articles
- Count ‘d’ digit positive integers with 0 as a digit in C++
- Count numbers having 0 as a digit in C++
- Program to count n digit integers where digits are strictly increasing in Python
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Rewrite the following integers as rational numbers.a. $-3$ b. 0 c. 1 d. 99
- Compare natural numbers and positive integers.
- On a number line, the positive integers are present on which side of 0?
- Count numbers with same first and last digits in C++
- Count Numbers with Unique Digits in C++
- Count of m digit integers that are divisible by an integer n in C++
- Count Numbers with N digits which consists of even number of 0's in C++
- Count Numbers with N digits which consists of odd number of 0's in C++
- Sum a negative number (negative and positive digits) - JavaScript
- Counting n digit Numbers with all unique digits in JavaScript
- The difference between the greatest five-digit number and the greatest five-digit number with all different digits is:(a) 1000 (b) 12345(c) 1 (d) 1234
