# Number of trailing zeros in N * (N – 2) * (N – 4)*….

Given an integer N, the task is to find the number of trailing zeros in the decimal notation of f(N) where f(N) = 1 if N < 2 and f(N) = N * f(N – 2) if N ≥ 2

Examples:

Input: N = 12
Output: 1
f(12) = 12 * 10 * 8 * 6 * 4 * 2 = 46080

Input: N = 7
Output: 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The number of trailing zeros when f(N) is expressed in decimal notation is the number of times f(N) is divisible by 2 and the number of times f(N) is divisible by 5. There are two cases:

1. When N is odd then f(N) is the product of some odd numbers, so it does not break at 2. So the answer is always 0.
2. When N is even then f(N) can be represented as 2 (1 * 2 * 3 * …. * N/2). The number of times f(N) is divisible by 2 is greater than the number of times divisible by 5, so only consider the number of times divisible by 5. Now, this problem is similar to count trailing zeroes in factorial of a number.

Below is the implementation of the above approach:

## CPP

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the count of ` `// trailing 0s in the given function ` `int` `findTrailingZeros(``int` `n) ` `{ ` `    ``// If n is odd ` `    ``if` `(n & 1) ` `        ``return` `0; ` ` `  `    ``// If n is even ` `    ``else` `{ ` `        ``int` `ans = 0; ` ` `  `        ``// Find the trailing zeros ` `        ``// in n/2 factorial ` `        ``n /= 2; ` `        ``while` `(n) { ` `            ``ans += n / 5; ` `            ``n /= 5; ` `        ``} ` ` `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 12; ` ` `  `    ``cout << findTrailingZeros(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return the count of  ` `    ``// trailing 0s in the given function  ` `    ``static` `int` `findTrailingZeros(``int` `n)  ` `    ``{  ` `        ``// If n is odd  ` `        ``if` `((n & ``1``) == ``1``)  ` `            ``return` `0``;  ` `     `  `        ``// If n is even  ` `        ``else`  `        ``{  ` `            ``int` `ans = ``0``;  ` `     `  `            ``// Find the trailing zeros  ` `            ``// in n/2 factorial  ` `            ``n /= ``2``;  ` `            ``while` `(n != ``0``) ` `            ``{  ` `                ``ans += n / ``5``;  ` `                ``n /= ``5``;  ` `            ``}  ` `     `  `            ``// Return the required answer  ` `            ``return` `ans;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `main (String[] args) ` `    ``{  ` `        ``int` `n = ``12``;  ` `     `  `        ``System.out.println(findTrailingZeros(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by AnkitRai01 `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the count of ` `# trailing 0s in the given function ` `def` `findTrailingZeros(n): ` `     `  `    ``# If n is odd ` `    ``if` `(n & ``1``): ` `        ``return` `0` ` `  `    ``# If n is even ` `    ``else``: ` `        ``ans ``=` `0` ` `  `        ``# Find the trailing zeros ` `        ``# in n/2 factorial ` `        ``n ``/``/``=` `2` `        ``while` `(n): ` `            ``ans ``+``=` `n ``/``/` `5` `            ``n ``/``/``=` `5` ` `  `        ``# Return the required answer ` `        ``return` `ans ` ` `  `# Driver code ` ` `  `n ``=` `12` ` `  `print``(findTrailingZeros(n)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `     `  `    ``// Function to return the count of  ` `    ``// trailing 0s in the given function  ` `    ``static` `int` `findTrailingZeros(``int` `n)  ` `    ``{  ` `        ``// If n is odd  ` `        ``if` `((n & 1) == 1)  ` `            ``return` `0;  ` `     `  `        ``// If n is even  ` `        ``else` `        ``{  ` `            ``int` `ans = 0;  ` `     `  `            ``// Find the trailing zeros  ` `            ``// in n/2 factorial  ` `            ``n /= 2;  ` `            ``while` `(n != 0) ` `            ``{  ` `                ``ans += n / 5;  ` `                ``n /= 5;  ` `            ``}  ` `     `  `            ``// Return the required answer  ` `            ``return` `ans;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{  ` `        ``int` `n = 12;  ` `     `  `        ``Console.WriteLine(findTrailingZeros(n));  ` `    ``}  ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```1
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.