# Count Numbers with N digits which consists of odd number of 0’s

We are given a number N. The task is to find the count of numbers which have N digits and odd number of zeroes.

**Note**: The number can have preceding 0’s.

**Examples**:

Input : N = 2 Output : Count = 18 Input : N = 3 Output : Count = 244

Suppose a number with N digits which contains only single zero. So the digits in the number as zero can be filled in only 1 way and the rest of each of the positions can be filled in 9 different ways with numbers from 1 to 9. So count of all such numbers with N digits and only 1 zero = ** ^{N}C_{1}*(9^{N-1})**.

Similarily, count of all such numbers with N digits and 3 zeroes = ** ^{N}C_{3}*(9^{N-3})**.

and so on.

So, count of all such numbers with N digits and odd number of zeroes will be,

+^{N}C_{1}*(9^{N-1})+^{N}C_{3}*(9^{N-3})+…….+^{N}C_{5}*(9^{N-5})^{N}C_{K}*(9^{N-K})Where, K is an odd number less than N.

The above equation can be written as,

(9^{N})((^{N}C_{1}* (1/9)) + (^{N}C_{3}* (1/9^3)) + (^{N}C_{5}* (1/9^5)) +…

The above equation can be represented as subtraction of two series, (9^{N})*{(1+x)^{N}-(1-x)^{N}}/2, where **x = 1/9**

Which is equal to,

(10^{N}- 8^{N})/2

Below is the implementation of the above approach:

## C++

`// C++ program to count numbers with N digits ` `// which consists of odd number of 0's ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count Numbers with N digits ` `// which consists of odd number of 0's ` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `(` `pow` `(10, N) - ` `pow` `(8, N)) / 2; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << countNumbers(n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count numbers ` `// with N digits which consists ` `// of odd number of 0's ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count Numbers ` `// with N digits which consists ` `// of odd number of 0's ` `static` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(Math.pow(` `10` `, N) - ` ` ` `Math.pow(` `8` `, N)) / ` `2` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(countNumbers(n)); ` `} ` `} ` ` ` `// This code is contributed by Shashank ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to count numbers ` `# with N digits which consists of ` `# odd number of 0's ` ` ` `# Function to count Numbers with ` `# N digits which consists of odd ` `# number of 0's ` `def` `countNumbers( N): ` ` ` ` ` `return` `(` `pow` `(` `10` `, N) ` `-` `pow` `(` `8` `, N)) ` `/` `/` `2` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` ` ` ` ` `print` `(countNumbers(n)) ` ` ` `# This code is contributed ` `# by ChitraNayal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count numbers ` `// with N digits which consists ` `// of odd number of 0's ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count Numbers ` `// with N digits which consists ` `// of odd number of 0's ` `static` `int` `countNumbers(` `int` `N) ` `{ ` ` ` `return` `(` `int` `)(Math.Pow(10, N) - ` ` ` `Math.Pow(8, N)) / 2; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(countNumbers(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai(Abby_akku) ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count numbers ` `// with N digits which consists ` `// of odd number of 0's ` ` ` `// Function to count Numbers ` `// with N digits which consists ` `// of odd number of 0's ` `function` `countNumbers(` `$N` `) ` `{ ` ` ` `return` `(pow(10, ` `$N` `) - ` ` ` `pow(8, ` `$N` `)) / 2; ` `} ` ` ` `// Driver code ` `$n` `= 5; ` ` ` `echo` `countNumbers(` `$n` `) ; ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

33616

**Note**: Answer can be very large, so for N greater than 9, use modular exponentiation.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count Numbers with N digits which consists of even number of 0’s
- Count of subsequences which consists exactly K prime numbers
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count of Numbers such that difference between the number and sum of its digits not less than L
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count the number of digits of palindrome numbers in an array
- Count of numbers in Array ending with digits of number N
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Smallest multiple of 3 which consists of three given non-zero digits
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count numbers with same first and last digits
- Count numbers less than N containing digits from the given set : Digit DP
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of numbers from range [L, R] that end with any of the given digits
- Count of numbers with all digits same in a given range
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of N-digit numbers with all distinct digits

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.