Skip to content
Related Articles

Related Articles

Number of trailing zeros in N * (N – 2) * (N – 4)*….
  • Last Updated : 21 Jan, 2020

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :