Count how many integers from 1 to N contains 0’s as a digit.

Examples:

Input: n = 9 Output: 0 Input: n = 107 Output: 17 The numbers having 0 are 10, 20,..90, 100, 101..107 Input: n = 155 Output: 24 The numbers having 0 are 10, 20,..90, 100, 101..110, 120, ..150.

The idea is to traverse all numbers from 1 to n. For every traversed number, traverse through its digits, if any digit is 0, increment count. Below is the implementation of the above idea :

## C++

`// C++ program to count numbers from 1 to n with ` `// 0 as a digit ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns 1 if x has 0, else 0 ` `int` `has0(` `int` `x) ` `{ ` ` ` `// Traverse through all digits of ` ` ` `// x to check if it has 0. ` ` ` `while` `(x) ` ` ` `{ ` ` ` `// If current digit is 0, return true ` ` ` `if` `(x % 10 == 0) ` ` ` `return` `1; ` ` ` ` ` `x /= 10; ` ` ` `} ` ` ` ` ` `return` `0; ` `} ` ` ` `// Returns count of numbers from 1 to n with 0 as digit ` `int` `getCount(` `int` `n) ` `{ ` ` ` `// Initialize count of numbers having 0 as digit ` ` ` `int` `count = 0; ` ` ` ` ` `// Traverse through all numbers and for every number ` ` ` `// check if it has 0. ` ` ` `for` `(` `int` `i=1; i<=n; i++) ` ` ` `count += has0(i); ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 107; ` ` ` `cout << ` `"Count of numbers from 1"` `<< ` `" to "` ` ` `<< n << ` `" is "` `<< getCount(n); ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count numbers ` `// from 1 to n with 0 as a digit ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns 1 if x has 0, else 0 ` ` ` `static` `int` `has0(` `int` `x) ` ` ` `{ ` ` ` `// Traverse through all digits ` ` ` `// of x to check if it has 0. ` ` ` `while` `(x != ` `0` `) ` ` ` `{ ` ` ` `// If current digit is 0, ` ` ` `// return true ` ` ` `if` `(x % ` `10` `== ` `0` `) ` ` ` `return` `1` `; ` ` ` ` ` `x /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `0` `; ` ` ` `} ` ` ` ` ` `// Returns count of numbers ` ` ` `// from 1 to n with 0 as digit ` ` ` `static` `int` `getCount(` `int` `n) ` ` ` `{ ` ` ` `// Initialize count of ` ` ` `// numbers having 0 as digit ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Traverse through all numbers ` ` ` `// and for every number ` ` ` `// check if it has 0. ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `count += has0(i); ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver program ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `107` `; ` ` ` `System.out.println(` `"Count of numbers from 1"` ` ` `+ ` `" to "` `+n + ` `" is "` `+ getCount(n)); ` `} ` `} ` ` ` `// This code is contributed by Nikita Tiwari. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count numbers ` `# from 1 to n with 0 as a digit ` ` ` `# Returns 1 if x has 0, else 0 ` `def` `has0(x) : ` ` ` ` ` `# Traverse through all digits ` ` ` `# of x to check if it has 0. ` ` ` `while` `(x !` `=` `0` `) : ` ` ` ` ` `# If current digit is 0, ` ` ` `# return true ` ` ` `if` `(x ` `%` `10` `=` `=` `0` `) : ` ` ` `return` `1` ` ` ` ` `x ` `=` `x ` `/` `/` `10` ` ` ` ` `return` `0` ` ` ` ` `# Returns count of numbers ` `# from 1 to n with 0 as digit ` `def` `getCount(n) : ` ` ` ` ` `# Initialize count of numbers ` ` ` `# having 0 as digit. ` ` ` `count ` `=` `0` ` ` ` ` `# Traverse through all numbers ` ` ` `# and for every number check ` ` ` `# if it has 0. ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `) : ` ` ` `count ` `=` `count ` `+` `has0(i) ` ` ` ` ` `return` `count ` ` ` ` ` `# Driver program ` `n ` `=` `107` `print` `(` `"Count of numbers from 1"` `, ` `" to "` `, ` ` ` `n , ` `" is "` `, getCount(n)) ` ` ` ` ` `# This code is contributed by Nikita tiwari. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count numbers ` `// from 1 to n with 0 as a digit ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Returns 1 if x has 0, else 0 ` ` ` `static` `int` `has0(` `int` `x) ` ` ` `{ ` ` ` `// Traverse through all digits ` ` ` `// of x to check if it has 0. ` ` ` `while` `(x != 0) ` ` ` `{ ` ` ` `// If current digit is 0, ` ` ` `// return true ` ` ` `if` `(x % 10 == 0) ` ` ` `return` `1; ` ` ` ` ` `x /= 10; ` ` ` `} ` ` ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `// Returns count of numbers ` ` ` `// from 1 to n with 0 as digit ` ` ` `static` `int` `getCount(` `int` `n) ` ` ` `{ ` ` ` `// Initialize count of ` ` ` `// numbers having 0 as digit ` ` ` `int` `count = 0; ` ` ` ` ` `// Traverse through all numbers ` ` ` `// and for every number ` ` ` `// check if it has 0. ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `count += has0(i); ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` ` ` `int` `n = 107; ` ` ` `Console.WriteLine(` `"Count of numbers from 1"` ` ` `+ ` `" to "` `+n + ` `" is "` `+ getCount(n)); ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

Output:

Count of numbers from 1 to 107 is 17

Refer below post for an optimized solution.

Count numbers having 0 as a digit

This article is contributed by Dheeraj Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of N-digit numbers having digit XOR as single digit
- Count n digit numbers not having a particular digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers having 0 as a digit
- Count numbers formed by given two digit with sum having given digits
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count all numbers up to N having M as the last digit
- Count three-digit numbers having difference X with its reverse
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count 'd' digit positive integers with 0 as a digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Numbers having difference with digit sum more than s
- Generate all N digit numbers having absolute difference as K between adjacent digits
- N digit numbers having difference between the first and last digits as K
- Find a number K having sum of numbers obtained by repeated removal of last digit of K is N
- Count non-palindromic array elements having same first and last digit
- Check if frequency of each digit is less than the digit
- Largest number less than N with digit sum greater than the digit sum of N