Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Find the Number of Trailing Zeroes in N!

Basic Acceptance 83.63% Points 10

You are given a non-negative integer N. Your task is to return the number of trailing zeroes in N! (N factorial).

A trailing zero is formed by a factor of 10, which is the product of 2 and 5. Since factorials contain more factors of 2 than factors of 5, the number of trailing zeroes can be determined by counting the total number of factors of 5 in N!.

Examples
Example 1

Input: N = 5

Output: 1

Explanation: 5! = 120 has exactly one trailing zero.

Example 2

Input: N = 100

Output: 24

Explanation: The number of trailing zeroes is calculated by counting the multiples of powers of 5: 100 / 5 + 100 / 25 = 20 + 4 = 24.

Example 3

Input: N = 3

Output: 0

Explanation: 3! = 6 has no trailing zeroes.

Constraints
  • 0 <= N <= 109
Topics
Introduction Data Structures Basics Basics
Companies
Apple DE Shaw
Added 05 Nov 2026 83.63% acceptance
Ln 1, Col 1Sign in to save your code
5
1