Given a number **N**, the task is to write C program to count the number of zeros from **0 to N**.

**Examples:**

Input:N = 10

Output:2

Explanation:

The number with zeros are 0, 10 till 10. Hence the count of zeros is 2.

Input:N = 20

Output:3

Explanation:

The number with zeros are 0, 10, 20 till 20. Hence the count of zeros is 3.

**Approach:**

- Iterate from 0 to N.
- For each number do the following:
- Store the above number in a variable
**temp**. - For each digit in temp, if the digit is zero then increment the zero count by 1.

- Store the above number in a variable
- Print the count of zeros calculated in the above steps.

Below is the implementation of the above approach:

`// C program for the above approach ` `#include <stdio.h> ` ` ` `// Function to count zero in temp ` `int` `countZero(` `int` `temp) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `// Iterate each digit in temp ` ` ` `while` `(temp) { ` ` ` ` ` `// If digit is zero, increment ` ` ` `// the count ` ` ` `if` `(temp % 10 == 0) ` ` ` `cnt++; ` ` ` ` ` `temp /= 10; ` ` ` `} ` ` ` ` ` `// Return the final count ` ` ` `return` `cnt; ` `} ` ` ` `// Function that counts the number of ` `// zeros from 0 to N ` `void` `countZerostillN(` `int` `N) ` `{ ` ` ` `// To store the finalCount of zero ` ` ` `int` `finalCount = 1; ` ` ` ` ` `// Iterate from 0 to N ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `// Function call to count the ` ` ` `// zeros in variable i ` ` ` `finalCount += countZero(i); ` ` ` `} ` ` ` ` ` `// Print the final Count of zero ` ` ` `printf` `(` `"%d"` `, finalCount); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given number ` ` ` `int` `N = 20; ` ` ` ` ` `// Function Call ` ` ` `countZerostillN(N); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** *O(N)*

**Auxiliary Space:** *O(1)*

## Recommended Posts:

- C program to count zeros and ones in binary representation of a number
- Smallest number divisible by n and has at-least k trailing zeros
- Check if any permutation of a number without any leading zeros is a power of 2 or not
- Remove leading zeros from an array
- All possible numbers of N digits and base B without leading zeros
- Sum of all elements in an array between zeros
- Program to count number of distinct Squares and Cubes upto N
- C program to count number of vowels and consonants in a String
- C program to Count the digits of a number
- Find count of digits in a number that divide the number
- Count Number of animals in a zoo from given number of head and legs
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count the number of occurrences of a particular digit in a number
- Find smallest possible Number from a given large Number with same count of digits
- Count of prime digits of a Number which divides the number
- Program to check whether the given number is Buzz Number or not
- Check if count of Alphabets and count of Numbers are equal in the given String
- Check if count of even divisors of N is equal to count of odd divisors
- Program to count vowels in a string (Iterative and Recursive)
- Program to Count numbers on fingers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.