# Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*..

Given an integer **n**, the task is to find the number of trailing zeros in the function i.e. **f(n) = 1 ^{1} * 2^{2} * 3^{3} * … * n^{n}**.

**Examples:**

Input:n = 5

Output:5

f(5) = 1^{1}* 2^{2}* 3^{3}* 4^{4}* 5^{5}= 1 * 4 * 27 * 256 * 3125 = 86400000

Input:n = 12

Output:15

**Approach:** We know that **5 * 2 = 10** i.e. 1 trailing zero is the result of the multiplication of a single 5 and a single 2. So, if we have **x** number of **5** and **y** number of **2** then the number of trailing zeros will be **min(x, y)**.

Now, for every number **i** in the series, we need to count the number of **2** and **5** in its factors say **x** and **y** but the number of **2s** and **5s** will be **x * i** and **y * i** respectively because in the series **i** is raised to the power itself i.e. **i ^{i}**. Count the number of

**2s**and

**5s**in the complete series and print the minimum of them which is the required answer.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of ` `// trailing zeros ` `int` `trailing_zeros(` `int` `N) ` `{ ` ` ` ` ` `// To store the number of 2s and 5s ` ` ` `int` `count_of_two = 0, count_of_five = 0; ` ` ` ` ` `for` `(` `int` `i = 1; i <= N; i++) { ` ` ` ` ` `int` `val = i; ` ` ` ` ` `while` `(val % 2 == 0 && val > 0) { ` ` ` `val /= 2; ` ` ` ` ` `// If we get a factor 2 then we ` ` ` `// have i number of 2s because ` ` ` `// the power of the number is ` ` ` `// raised to i ` ` ` `count_of_two += i; ` ` ` `} ` ` ` ` ` `while` `(val % 5 == 0 && val > 0) { ` ` ` `val /= 5; ` ` ` ` ` `// If we get a factor 5 then ` ` ` `// we have i number of 5s ` ` ` `// because the power of the ` ` ` `// number is raised to i ` ` ` `count_of_five += i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Take the minimum of them ` ` ` `int` `ans = min(count_of_two, count_of_five); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 12; ` ` ` ` ` `cout << trailing_zeros(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// trailing zeros ` `static` `int` `trailing_zeros(` `int` `N) ` `{ ` ` ` ` ` `// To store the number of 2s and 5s ` ` ` `int` `count_of_two = ` `0` `, count_of_five = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= N; i++) ` ` ` `{ ` ` ` `int` `val = i; ` ` ` `while` `(val % ` `2` `== ` `0` `&& val > ` `0` `) ` ` ` `{ ` ` ` `val /= ` `2` `; ` ` ` ` ` `// If we get a factor 2 then we ` ` ` `// have i number of 2s because ` ` ` `// the power of the number is ` ` ` `// raised to i ` ` ` `count_of_two += i; ` ` ` `} ` ` ` ` ` `while` `(val % ` `5` `== ` `0` `&& val > ` `0` `) ` ` ` `{ ` ` ` `val /= ` `5` `; ` ` ` ` ` `// If we get a factor 5 then ` ` ` `// we have i number of 5s ` ` ` `// because the power of the ` ` ` `// number is raised to i ` ` ` `count_of_five += i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Take the minimum of them ` ` ` `int` `ans = Math.min(count_of_two, count_of_five); ` ` ` ` ` `return` `ans; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `N = ` `12` `; ` ` ` `System.out.println(trailing_zeros(N)); ` `} ` `} ` ` ` `// This code is contributed by chandan_jnu ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` ` ` `# Function to return the number of ` `# trailing zeros ` `def` `trailing_zeros(N): ` ` ` ` ` `# To store the number of 2s and 5s ` ` ` `count_of_two ` `=` `0` ` ` `count_of_five ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `1` `, N ` `+` `1` `, ` `1` `): ` ` ` `val ` `=` `i ` ` ` ` ` `while` `(val ` `%` `2` `=` `=` `0` `and` `val > ` `0` `): ` ` ` `val ` `/` `=` `2` ` ` ` ` `# If we get a factor 2 then we ` ` ` `# have i number of 2s because ` ` ` `# the power of the number is ` ` ` `# raised to i ` ` ` `count_of_two ` `+` `=` `i ` ` ` ` ` `while` `(val ` `%` `5` `=` `=` `0` `and` `val > ` `0` `): ` ` ` `val ` `/` `=` `5` ` ` ` ` `# If we get a factor 5 then we ` ` ` `# have i number of 5s because ` ` ` `# the power of the number is ` ` ` `# raised to i ` ` ` `count_of_five ` `+` `=` `i ` ` ` ` ` `# Take the minimum of them ` ` ` `ans ` `=` `min` `(count_of_two, count_of_five) ` ` ` ` ` `return` `ans ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `12` ` ` ` ` `print` `(trailing_zeros(N)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the number of ` ` ` `// trailing zeros ` ` ` `static` `int` `trailing_zeros(` `int` `N) ` ` ` `{ ` ` ` ` ` `// To store the number of 2s and 5s ` ` ` `int` `count_of_two = 0, count_of_five = 0; ` ` ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `{ ` ` ` `int` `val = i; ` ` ` `while` `(val % 2 == 0 && val > 0) ` ` ` `{ ` ` ` `val /= 2; ` ` ` ` ` `// If we get a factor 2 then we ` ` ` `// have i number of 2s because ` ` ` `// the power of the number is ` ` ` `// raised to i ` ` ` `count_of_two += i; ` ` ` `} ` ` ` ` ` `while` `(val % 5 == 0 && val > 0) ` ` ` `{ ` ` ` `val /= 5; ` ` ` ` ` `// If we get a factor 5 then ` ` ` `// we have i number of 5s ` ` ` `// because the power of the ` ` ` `// number is raised to i ` ` ` `count_of_five += i; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Take the minimum of them ` ` ` `int` `ans = Math.Min(count_of_two, count_of_five); ` ` ` ` ` `return` `ans; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 12; ` ` ` `Console.WriteLine(trailing_zeros(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

0)

{

$val /= 2;

// If we get a factor 2 then we

// have i number of 2s because

// the power of the number is

// raised to i

$count_of_two += $i;

}

while ($val % 5 == 0 && $val > 0)

{

$val /= 5;

// If we get a factor 5 then

// we have i number of 5s

// because the power of the

// number is raised to i

$count_of_five += $i;

}

}

// Take the minimum of them

$ans = min($count_of_two, $count_of_five);

return $ans;

}

// Driver code

$N = 12;

echo trailing_zeros($N);

// This code is contributed by ita_c

?>

**Output:**

15

## Recommended Posts:

- Count number of trailing zeros in product of array
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count unique numbers that can be generated from N by adding one and removing trailing zeros
- Smallest number divisible by n and has at-least k trailing zeros
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Count trailing zeroes in factorial of a number
- Count numbers having N 0's and and M 1's with no leading zeros
- Trailing number of 0s in product of two factorials
- Smallest number with at least n trailing zeroes in factorial
- Number of trailing zeroes in base B representation of N!
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Check if the given array can be reduced to zeros with the given operation performed given number of times
- Count number of triplets with product equal to given number with duplicates allowed
- Count Number of animals in a zoo from given number of head and legs
- Find count of digits in a number that divide the number

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.