Count numbers having 0 as a digit
Last Updated :
23 Mar, 2023
Count how many integers from 1 to N contain 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);
}
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.
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.
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
JavaScript
<script>
// JavaScript program to count numbers from 1 to n with
// 0 as a digit
// Returns 1 if x has 0, else 0
function has0(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 = Math.floor(x / 10);
}
return 0;
}
// Returns count of numbers from 1 to n with 0 as digit
function getCount(n)
{
// Initialize count of numbers having 0 as digit
let count = 0;
// Traverse through all numbers and for every number
// check if it has 0.
for (let i=1; i<=n; i++)
count += has0(i);
return count;
}
// Driver program
let n = 107;
document.write("Count of numbers from 1" + " to "
+ n + " is " + getCount(n));
// This code is contributed by Surbhi Tyagi.
</script>
OutputCount of numbers from 1 to 107 is 17
Time Complexity: O(n log n)
Auxiliary Space: O(1)
Refer below post for an optimized solution.
Count numbers having 0 as a digit
Similar Reads
Count n digit numbers not having a particular digit We are given two integers n and d, we need to count all n digit numbers that do not have digit d. Example : Input : n = 2, d = 7 Output : 72 All two digit numbers that don't have 7 as digit are 10, 11, 12, 13, 14, 15, 16, 18, ..... Input : n = 3, d = 9 Output : 648 A simple solution is to traverse t
8 min read
Count of unique digits in a given number N Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Count of unique digits in a given number N Given a number N, the task is to count the number of unique digits in the given number. Examples: Input: N = 22342 Output: 2 Explanation:The digits 3 and 4 occurs only once. Hence, the output is 2. Input: N = 99677 Output: 1Explanation:The digit 6 occurs only once. Hence, the output is 1. Naive Appr
6 min read
Count 'd' digit positive integers with 0 as a digit Given a number d, representing the number of digits of a positive integer. Find the total count of positive integer (consisting of d digits exactly) which have at-least one zero in them.Examples: Input : d = 1 Output : 0 There's no natural number of 1 digit that contains a zero. Input : d = 2 Output
5 min read
Count of Binary Digit numbers smaller than N Given a limit N, we need to find out the count of binary digit numbers which are smaller than N. Binary digit numbers are those numbers that contain only 0 and 1 as their digits, like 1, 10, 101, etc are binary digit numbers. Examples: Input : N = 200 Output : 7 Count of binary digit number smaller
6 min read
Count number of 0's with given conditions Given a binary string (containing 1's and 0's), write a program to count the number of 0's in a given string such that the following conditions hold: 1's and 0's are in any order in a stringUse of conditional statements like if, if..else, and switch are not allowedUse of addition/subtraction is not
5 min read